home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / phpmyadmin / server_privileges.php < prev    next >
Encoding:
PHP Script  |  2006-11-18  |  110.0 KB  |  2,171 lines

  1. <?php
  2. /* $Id: server_privileges.php 9197 2006-07-26 20:41:59Z lem9 $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. require_once('./libraries/common.lib.php');
  6.  
  7. /**
  8.  * Does the common work
  9.  */
  10. $js_to_run = 'server_privileges.js';
  11. require('./libraries/server_common.inc.php');
  12.  
  13.  
  14. /**
  15.  * Checks if a dropdown box has been used for selecting a database / table
  16.  */
  17. if (isset($pred_dbname) && strlen($pred_dbname)) {
  18.     $dbname = $pred_dbname;
  19.     unset($pred_dbname);
  20. }
  21. if (isset($pred_tablename) && strlen($pred_tablename)) {
  22.     $tablename = $pred_tablename;
  23.     unset($pred_tablename);
  24. }
  25.  
  26. // check if given $dbanem is a wildcard or not
  27. if ( isset( $dbname ) ) {
  28.     //if ( preg_match( '/\\\\(?:_|%)/i', $dbname ) ) {
  29.     if ( preg_match( '/(?<!\\\\)(?:_|%)/i', $dbname ) ) {
  30.         $dbname_is_wildcard = true;
  31.     } else {
  32.         $dbname_is_wildcard = false;
  33.     }
  34. }
  35.  
  36. /**
  37.  * Checks if the user is allowed to do what he tries to...
  38.  */
  39. if (!$is_superuser) {
  40.     require('./libraries/server_links.inc.php');
  41.     echo '<h2>' . "\n"
  42.        . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
  43.        . $GLOBALS['strPrivileges'] . "\n"
  44.        . '</h2>' . "\n"
  45.        . $GLOBALS['strNoPrivileges'] . "\n";
  46.     require_once('./libraries/footer.inc.php');
  47. }
  48.  
  49. /**
  50.  * Generates a condition on the user name
  51.  *
  52.  * @param   string   the user's initial
  53.  * @return  string   the generated condition
  54.  */
  55. function PMA_RangeOfUsers($initial = '') {
  56. // strtolower() is used because the User field
  57. // might be BINARY, so LIKE would be case sensitive
  58.     if (!empty($initial)) {
  59.         $ret = " WHERE " . PMA_convert_using('User')
  60.          . " LIKE " . PMA_convert_using($initial . '%', 'quoted')
  61.          . " OR ". PMA_convert_using('User')
  62.          . " LIKE " . PMA_convert_using(strtolower($initial) . '%', 'quoted');
  63.     } else {
  64.         $ret = '';
  65.     }
  66.     return $ret;
  67. } // end function
  68.  
  69. /**
  70.  * Extracts the privilege information of a priv table row
  71.  *
  72.  * @param   array   $row        the row
  73.  * @param   boolean $enableHTML add <dfn> tag with tooltips
  74.  *
  75.  * @global  ressource $user_link the database connection
  76.  *
  77.  * @return  array
  78.  */
  79. function PMA_extractPrivInfo($row = '', $enableHTML = FALSE)
  80. {
  81.     $grants = array(
  82.         array('Select_priv', 'SELECT', $GLOBALS['strPrivDescSelect']),
  83.         array('Insert_priv', 'INSERT', $GLOBALS['strPrivDescInsert']),
  84.         array('Update_priv', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
  85.         array('Delete_priv', 'DELETE', $GLOBALS['strPrivDescDelete']),
  86.         array('Create_priv', 'CREATE', $GLOBALS['strPrivDescCreateDb']),
  87.         array('Drop_priv', 'DROP', $GLOBALS['strPrivDescDropDb']),
  88.         array('Reload_priv', 'RELOAD', $GLOBALS['strPrivDescReload']),
  89.         array('Shutdown_priv', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']),
  90.         array('Process_priv', 'PROCESS', $GLOBALS['strPrivDescProcess' . ((!empty($row) && isset($row['Super_priv'])) || (empty($row) && isset($GLOBALS['Super_priv'])) ? '4' : '3')]),
  91.         array('File_priv', 'FILE', $GLOBALS['strPrivDescFile']),
  92.         array('References_priv', 'REFERENCES', $GLOBALS['strPrivDescReferences']),
  93.         array('Index_priv', 'INDEX', $GLOBALS['strPrivDescIndex']),
  94.         array('Alter_priv', 'ALTER', $GLOBALS['strPrivDescAlter']),
  95.         array('Show_db_priv', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']),
  96.         array('Super_priv', 'SUPER', $GLOBALS['strPrivDescSuper']),
  97.         array('Create_tmp_table_priv', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']),
  98.         array('Lock_tables_priv', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']),
  99.         array('Repl_slave_priv', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']),
  100.         array('Repl_client_priv', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']),
  101.         array('Create_view_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
  102.         // for table privs:
  103.         array('Create View_priv', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']),
  104.         array('Show_view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
  105.         // for table privs:
  106.         array('Show view_priv', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']),
  107.         array('Create_routine_priv', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']),
  108.         array('Alter_routine_priv', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']),
  109.         array('Create_user_priv', 'CREATE USER', $GLOBALS['strPrivDescCreateUser'])
  110.     );
  111.     if (PMA_MYSQL_INT_VERSION >= 40002 && PMA_MYSQL_INT_VERSION <50003) {
  112.         $grants[] = array('Execute_priv', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
  113.     } else {
  114.         $grants[] = array('Execute_priv', 'EXECUTE', $GLOBALS['strPrivDescExecute5']);
  115.     }
  116.  
  117.     if (!empty($row) && isset($row['Table_priv'])) {
  118.         $res = PMA_DBI_query(
  119.             'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
  120.             $GLOBALS['userlink'] );
  121.         $row1 = PMA_DBI_fetch_assoc($res);
  122.         PMA_DBI_free_result($res);
  123.         $av_grants = explode ('\',\'', substr($row1['Type'], 5, strlen($row1['Type']) - 7));
  124.         unset($row1);
  125.         $users_grants = explode(',', $row['Table_priv']);
  126.         foreach ($av_grants as $current_grant) {
  127.             $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
  128.         }
  129.         unset($current_grant);
  130.         unset($av_grants);
  131.         unset($users_grants);
  132.     }
  133.     $privs = array();
  134.     $allPrivileges = TRUE;
  135.     foreach ($grants as $current_grant) {
  136.         if ((!empty($row) && isset($row[$current_grant[0]])) || (empty($row) && isset($GLOBALS[$current_grant[0]]))) {
  137.             if ((!empty($row) && $row[$current_grant[0]] == 'Y') || (empty($row) && ($GLOBALS[$current_grant[0]] == 'Y' || (is_array($GLOBALS[$current_grant[0]]) && count($GLOBALS[$current_grant[0]]) == $GLOBALS['column_count'] && empty($GLOBALS[$current_grant[0] . '_none']))))) {
  138.                 if ($enableHTML) {
  139.                     $privs[] = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
  140.                 } else {
  141.                     $privs[] = $current_grant[1];
  142.                 }
  143.             } elseif (!empty($GLOBALS[$current_grant[0]]) && is_array($GLOBALS[$current_grant[0]]) && empty($GLOBALS[$current_grant[0] . '_none'])) {
  144.                 if ($enableHTML) {
  145.                     $priv_string = '<dfn title="' . $current_grant[2] . '">' . $current_grant[1] . '</dfn>';
  146.                 } else {
  147.                     $priv_string = $current_grant[1];
  148.                 }
  149.                 $privs[] = $priv_string . ' (`' . join('`, `', $GLOBALS[$current_grant[0]]) . '`)';
  150.             } else {
  151.                 $allPrivileges = FALSE;
  152.             }
  153.         }
  154.     }
  155.     if (empty($privs)) {
  156.         if ($enableHTML) {
  157.             $privs[] = '<dfn title="' . $GLOBALS['strPrivDescUsage'] . '">USAGE</dfn>';
  158.         } else {
  159.             $privs[] = 'USAGE';
  160.         }
  161.     } elseif ($allPrivileges && (!isset($GLOBALS['grant_count']) || count($privs) == $GLOBALS['grant_count'])) {
  162.         if ($enableHTML) {
  163.             $privs = array('<dfn title="' . $GLOBALS['strPrivDescAllPrivileges'] . '">ALL PRIVILEGES</dfn>');
  164.         } else {
  165.             $privs = array('ALL PRIVILEGES');
  166.         }
  167.     }
  168.     return $privs;
  169. } // end of the 'PMA_extractPrivInfo()' function
  170.  
  171.  
  172. /**
  173.  * Displays on which column(s) a table-specific privilege is granted
  174.  */
  175. function PMA_display_column_privs($spaces, $columns, $row, $name_for_select, $priv_for_header, $name, $name_for_dfn, $name_for_current) {
  176.  
  177.         echo $spaces . '    <div class="item" id="div_item_' . $name . '">' . "\n"
  178.            . $spaces . '        <label for="select_' . $name . '_priv">' . "\n"
  179.            . $spaces . '            <tt><dfn title="' . $name_for_dfn . '">' . $priv_for_header . '</dfn></tt>' . "\n"
  180.            . $spaces . '        </label>' . "\n"
  181.            . $spaces . '        <select id="select_' . $name . '_priv" name="' . $name_for_select . '[]" multiple="multiple">' . "\n";
  182.  
  183.         foreach ($columns as $current_column => $current_column_privileges) {
  184.             echo $spaces . '            <option value="' . htmlspecialchars($current_column) . '"';
  185.             if ($row[$name_for_select] == 'Y' || $current_column_privileges[$name_for_current]) {
  186.                 echo ' selected="selected"';
  187.             }
  188.             echo '>' . htmlspecialchars($current_column) . '</option>' . "\n";
  189.         }
  190.  
  191.         echo $spaces . '        </select>' . "\n"
  192.            . $spaces . '        <i>' . $GLOBALS['strOr'] . '</i>' . "\n"
  193.            . $spaces . '        <label for="checkbox_' . $name_for_select . '_none"><input type="checkbox"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' name="' . $name_for_select . '_none" id="checkbox_' . $name_for_select . '_none" title="' . $GLOBALS['strNone'] . '" />'
  194.            . $GLOBALS['strNone'] . '</label>' . "\n"
  195.            . $spaces . '    </div>' . "\n";
  196. } // end function
  197.  
  198. /**
  199.  * Displays the privileges form table
  200.  *
  201.  * @param   string  $db     the database
  202.  * @param   string  $table  the table
  203.  * @param   boolean $submit wheather to display the submit button or not
  204.  * @param   int     $indent the indenting level of the code
  205.  *
  206.  * @global  array      $cfg         the phpMyAdmin configuration
  207.  * @global  ressource  $user_link   the database connection
  208.  *
  209.  * @return  void
  210.  */
  211. function PMA_displayPrivTable($db = '*', $table = '*', $submit = TRUE, $indent = 0)
  212. {
  213.     if ($db == '*') {
  214.         $table = '*';
  215.     }
  216.     $spaces = str_repeat( '    ', $indent );
  217.  
  218.     if (isset($GLOBALS['username'])) {
  219.         $username = $GLOBALS['username'];
  220.         $hostname = $GLOBALS['hostname'];
  221.         if ($db == '*') {
  222.             $sql_query =
  223.                  'SELECT * FROM `mysql`.`user`'
  224.                 .' WHERE ' . PMA_convert_using('User')
  225.                 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  226.                 .' AND ' . PMA_convert_using('Host')
  227.                 .' = ' . PMA_convert_using($hostname, 'quoted') . ';';
  228.         } elseif ($table == '*') {
  229.             $sql_query =
  230.                 'SELECT * FROM `mysql`.`db`'
  231.                 .' WHERE ' . PMA_convert_using('`User`')
  232.                 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  233.                 .' AND ' . PMA_convert_using('`Host`')
  234.                 .' = ' . PMA_convert_using($hostname, 'quoted')
  235.                 .' AND ' .  PMA_convert_using( PMA_unescape_mysql_wildcards( $db ), 'quoted' )
  236.                 .' LIKE ' . PMA_convert_using( '`Db`' ) . ';';
  237.         } else {
  238.             $sql_query =
  239.                 'SELECT `Table_priv`'
  240.                 .' FROM `mysql`.`tables_priv`'
  241.                 .' WHERE ' . PMA_convert_using('`User`')
  242.                 .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  243.                 .' AND ' .PMA_convert_using('`Host`')
  244.                 .' = ' . PMA_convert_using( $hostname, 'quoted' )
  245.                 .' AND ' .PMA_convert_using('`Db`')
  246.                 .' = ' . PMA_convert_using( PMA_unescape_mysql_wildcards( $db ), 'quoted' )
  247.                 .' AND ' . PMA_convert_using('`Table_name`')
  248.                 .' = ' . PMA_convert_using($table, 'quoted') . ';';
  249.         }
  250.         $res = PMA_DBI_query($sql_query);
  251.         $row = PMA_DBI_fetch_assoc($res);
  252.         PMA_DBI_free_result($res);
  253.     }
  254.     if (empty($row)) {
  255.         if ($table == '*') {
  256.             if ($db == '*') {
  257.                 $sql_query = 'SHOW COLUMNS FROM `mysql`.`user`;';
  258.             } elseif ($table == '*') {
  259.                 $sql_query = 'SHOW COLUMNS FROM `mysql`.`db`;';
  260.             }
  261.             $res = PMA_DBI_query($sql_query);
  262.             while ($row1 = PMA_DBI_fetch_row($res)) {
  263.                 if (substr($row1[0], 0, 4) == 'max_') {
  264.                     $row[$row1[0]] = 0;
  265.                 } else {
  266.                     $row[$row1[0]] = 'N';
  267.                 }
  268.             }
  269.             PMA_DBI_free_result($res);
  270.         } else {
  271.             $row = array('Table_priv' => '');
  272.         }
  273.     }
  274.     if (isset($row['Table_priv'])) {
  275.         $res = PMA_DBI_query(
  276.             'SHOW COLUMNS FROM `mysql`.`tables_priv` LIKE \'Table_priv\';',
  277.             $GLOBALS['userlink'] );
  278.         // note: in MySQL 5.0.3 we get "Create View', 'Show view';
  279.         // the View for Create is spelled with uppercase V
  280.         // the view for Show is spelled with lowercase v
  281.         // and there is a space between the words
  282.  
  283.         $row1 = PMA_DBI_fetch_assoc($res);
  284.         PMA_DBI_free_result($res);
  285.         $av_grants = explode ('\',\'', substr($row1['Type'], strpos($row1['Type'], '(') + 2, strpos($row1['Type'], ')') - strpos($row1['Type'], '(') - 3));
  286.         unset($res, $row1);
  287.         $users_grants = explode(',', $row['Table_priv']);
  288.  
  289.         foreach ($av_grants as $current_grant) {
  290.             $row[$current_grant . '_priv'] = in_array($current_grant, $users_grants) ? 'Y' : 'N';
  291.         }
  292.         unset($row['Table_priv'], $current_grant, $av_grants, $users_grants);
  293.  
  294.         // get collumns
  295.         $res = PMA_DBI_try_query('SHOW COLUMNS FROM `' . PMA_unescape_mysql_wildcards( $db ) . '`.`' . $table . '`;');
  296.         $columns = array();
  297.         if ( $res ) {
  298.             while ($row1 = PMA_DBI_fetch_row($res)) {
  299.                 $columns[$row1[0]] = array(
  300.                     'Select' => FALSE,
  301.                     'Insert' => FALSE,
  302.                     'Update' => FALSE,
  303.                     'References' => FALSE
  304.                 );
  305.             }
  306.             PMA_DBI_free_result($res);
  307.         }
  308.         unset($res, $row1);
  309.     }
  310.     // t a b l e - s p e c i f i c    p r i v i l e g e s
  311.     if ( ! empty( $columns ) ) {
  312.         $res = PMA_DBI_query(
  313.             'SELECT `Column_name`, `Column_priv`'
  314.             .' FROM `mysql`.`columns_priv`'
  315.             .' WHERE ' . PMA_convert_using('`User`')
  316.             .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  317.             .' AND ' . PMA_convert_using('`Host`')
  318.             .' = ' . PMA_convert_using($hostname, 'quoted')
  319.             .' AND ' . PMA_convert_using('`Db`')
  320.             .' = ' . PMA_convert_using( PMA_unescape_mysql_wildcards( $db ), 'quoted')
  321.             .' AND ' . PMA_convert_using('`Table_name`')
  322.             .' = ' . PMA_convert_using($table, 'quoted') . ';');
  323.  
  324.         while ($row1 = PMA_DBI_fetch_row($res)) {
  325.             $row1[1] = explode(',', $row1[1]);
  326.             foreach ($row1[1] as $current) {
  327.                 $columns[$row1[0]][$current] = TRUE;
  328.             }
  329.         }
  330.         PMA_DBI_free_result($res);
  331.         unset( $res, $row1, $current );
  332.  
  333.         echo $spaces . '<input type="hidden" name="grant_count" value="' . count($row) . '" />' . "\n"
  334.            . $spaces . '<input type="hidden" name="column_count" value="' . count($columns) . '" />' . "\n"
  335.            . $spaces . '<fieldset id="fieldset_user_priv">' . "\n"
  336.            . $spaces . '    <legend>' . $GLOBALS['strTblPrivileges'] . '</legend>' . "\n"
  337.            . $spaces . '    <p><small><i>' . $GLOBALS['strEnglishPrivileges'] . '</i></small></p>' . "\n";
  338.  
  339.  
  340.         // privs that are attached to a specific column
  341.         PMA_display_column_privs($spaces, $columns, $row, 'Select_priv', 'SELECT', 'select', $GLOBALS['strPrivDescSelect'], 'Select');
  342.  
  343.         PMA_display_column_privs($spaces, $columns, $row, 'Insert_priv', 'INSERT', 'insert', $GLOBALS['strPrivDescInsert'], 'Insert');
  344.  
  345.         PMA_display_column_privs($spaces, $columns, $row, 'Update_priv', 'UPDATE', 'update', $GLOBALS['strPrivDescUpdate'], 'Update');
  346.  
  347.         PMA_display_column_privs($spaces, $columns, $row, 'References_priv', 'REFERENCES', 'references', $GLOBALS['strPrivDescReferences'], 'References');
  348.  
  349.         // privs that are not attached to a specific column
  350.  
  351.         echo $spaces . '    <div class="item">' . "\n";
  352.         foreach ($row as $current_grant => $current_grant_value) {
  353.             if (in_array(substr($current_grant, 0, (strlen($current_grant) - 5)), array('Select', 'Insert', 'Update', 'References'))) {
  354.                 continue;
  355.             }
  356.             // make a substitution to match the messages variables;
  357.             // also we must substitute the grant we get, because we can't generate
  358.             // a form variable containing blanks (those would get changed to
  359.             // an underscore when receiving the POST)
  360.             if ($current_grant == 'Create View_priv') {
  361.                 $tmp_current_grant = 'CreateView_priv';
  362.                 $current_grant = 'Create_view_priv';
  363.             } elseif ($current_grant == 'Show view_priv') {
  364.                 $tmp_current_grant = 'ShowView_priv';
  365.                 $current_grant = 'Show_view_priv';
  366.             } else {
  367.                 $tmp_current_grant = $current_grant;
  368.             }
  369.  
  370.             echo $spaces . '        <div class="item">' . "\n"
  371.                . $spaces . '            <input type="checkbox"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' name="' . $current_grant . '" id="checkbox_' . $current_grant . '" value="Y" ' . ($current_grant_value == 'Y' ? 'checked="checked" ' : '') . 'title="';
  372.  
  373.             echo (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]) ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))] : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '"/>' . "\n";
  374.  
  375.             echo $spaces . '            <label for="checkbox_' . $current_grant . '"><tt><dfn title="' . (isset($GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))]) ? $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5))] : $GLOBALS['strPrivDesc' . substr($tmp_current_grant, 0, (strlen($tmp_current_grant) - 5)) . 'Tbl']) . '">' . strtoupper(substr($current_grant, 0, strlen($current_grant) - 5)) . '</dfn></tt></label>' . "\n"
  376.                . $spaces . '        </div>' . "\n";
  377.         } // end foreach ()
  378.  
  379.         echo $spaces . '    </div>' . "\n";
  380.         // for Safari 2.0.2
  381.         echo $spaces . '    <div class="clearfloat"></div>' . "\n";
  382.  
  383.     } else {
  384.  
  385.         // g l o b a l    o r    d b - s p e c i f i c
  386.         //
  387.         // d a t a
  388.         $privTable[0] = array(
  389.             array('Select', 'SELECT', $GLOBALS['strPrivDescSelect']),
  390.             array('Insert', 'INSERT', $GLOBALS['strPrivDescInsert']),
  391.             array('Update', 'UPDATE', $GLOBALS['strPrivDescUpdate']),
  392.             array('Delete', 'DELETE', $GLOBALS['strPrivDescDelete'])
  393.         );
  394.         if ($db == '*') {
  395.             $privTable[0][] = array('File', 'FILE', $GLOBALS['strPrivDescFile']);
  396.         }
  397.  
  398.         // s t r u c t u r e
  399.         $privTable[1] = array(
  400.             array('Create', 'CREATE', ($table == '*' ? $GLOBALS['strPrivDescCreateDb'] : $GLOBALS['strPrivDescCreateTbl'])),
  401.             array('Alter', 'ALTER', $GLOBALS['strPrivDescAlter']),
  402.             array('Index', 'INDEX', $GLOBALS['strPrivDescIndex']),
  403.             array('Drop', 'DROP', ($table == '*' ? $GLOBALS['strPrivDescDropDb'] : $GLOBALS['strPrivDescDropTbl']))
  404.         );
  405.         if (isset($row['Create_tmp_table_priv'])) {
  406.             $privTable[1][] = array('Create_tmp_table', 'CREATE TEMPORARY TABLES', $GLOBALS['strPrivDescCreateTmpTable']);
  407.         }
  408.         // this one is for a db-specific priv: Create_view_priv
  409.         if (isset($row['Create_view_priv'])) {
  410.             $privTable[1][] = array('Create_view', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
  411.         }
  412.         // this one is for a table-specific priv: Create View_priv
  413.         if (isset($row['Create View_priv'])) {
  414.             $privTable[1][] = array('Create View', 'CREATE VIEW', $GLOBALS['strPrivDescCreateView']);
  415.         }
  416.         if (isset($row['Show_view_priv'])) {
  417.             $privTable[1][] = array('Show_view', 'SHOW VIEW', $GLOBALS['strPrivDescShowView']);
  418.         }
  419.         if (isset($row['Create_routine_priv'])) {
  420.             $privTable[1][] = array('Create_routine', 'CREATE ROUTINE', $GLOBALS['strPrivDescCreateRoutine']);
  421.         }
  422.         if (isset($row['Alter_routine_priv'])) {
  423.             $privTable[1][] = array('Alter_routine', 'ALTER ROUTINE', $GLOBALS['strPrivDescAlterRoutine']);
  424.         }
  425.         if (isset($row['Execute_priv'])) {
  426.             if (PMA_MYSQL_INT_VERSION >= 40002 && PMA_MYSQL_INT_VERSION <50003) {
  427.                 $privTable[1][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
  428.             } else {
  429.                 $privTable[1][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute5']);
  430.             }
  431.         }
  432.  
  433.         // a d m i n i s t r a t i o n
  434.         $privTable[2] = array();
  435.         if (isset($row['Grant_priv'])) {
  436.             $privTable[2][] = array('Grant', 'GRANT', $GLOBALS['strPrivDescGrant']);
  437.         }
  438.         if ($db == '*') {
  439.             if (isset($row['Super_priv'])) {
  440.                 $privTable[2][] = array('Super', 'SUPER', $GLOBALS['strPrivDescSuper']);
  441.                 $privTable[2][] = array('Process', 'PROCESS', $GLOBALS['strPrivDescProcess4']);
  442.             } else {
  443.                 $privTable[2][] = array('Process', 'PROCESS', $GLOBALS['strPrivDescProcess3']);
  444.             }
  445.             $privTable[2][] = array('Reload', 'RELOAD', $GLOBALS['strPrivDescReload']);
  446.             $privTable[2][] = array('Shutdown', 'SHUTDOWN', $GLOBALS['strPrivDescShutdown']);
  447.             if (isset($row['Show_db_priv'])) {
  448.                 $privTable[2][] = array('Show_db', 'SHOW DATABASES', $GLOBALS['strPrivDescShowDb']);
  449.             }
  450.         }
  451.         if (isset($row['Lock_tables_priv'])) {
  452.             $privTable[2][] = array('Lock_tables', 'LOCK TABLES', $GLOBALS['strPrivDescLockTables']);
  453.         }
  454.         $privTable[2][] = array('References', 'REFERENCES', $GLOBALS['strPrivDescReferences']);
  455.         if ($db == '*') {
  456.             //if (isset($row['Execute_priv'])) {
  457.             //    $privTable[2][] = array('Execute', 'EXECUTE', $GLOBALS['strPrivDescExecute']);
  458.             //}
  459.             if (isset($row['Repl_client_priv'])) {
  460.                 $privTable[2][] = array('Repl_client', 'REPLICATION CLIENT', $GLOBALS['strPrivDescReplClient']);
  461.             }
  462.             if (isset($row['Repl_slave_priv'])) {
  463.                 $privTable[2][] = array('Repl_slave', 'REPLICATION SLAVE', $GLOBALS['strPrivDescReplSlave']);
  464.             }
  465.             if (isset($row['Create_user_priv'])) {
  466.                 $privTable[2][] = array('Create_user', 'CREATE USER', $GLOBALS['strPrivDescCreateUser']);
  467.             }
  468.         }
  469.         echo $spaces . '<input type="hidden" name="grant_count" value="' . (count($privTable[0]) + count($privTable[1]) + count($privTable[2]) - (isset($row['Grant_priv']) ? 1 : 0)) . '" />' . "\n"
  470.            . $spaces . '<fieldset id="fieldset_user_global_rights">' . "\n"
  471.            . $spaces . '    <legend>' . "\n"
  472.            . $spaces . '        ' . ($db == '*' ? $GLOBALS['strGlobalPrivileges'] : ($table == '*' ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges'])) . "\n"
  473.            . $spaces . '        ( <a href="./server_privileges.php?' . $GLOBALS['url_query'] .  '&checkall=1" onclick="setCheckboxes(\'usersForm\', true); return false;">' . $GLOBALS['strCheckAll'] . '</a> /' . "\n"
  474.            . $spaces . '        <a href="./server_privileges.php?' . $GLOBALS['url_query'] .  '" onclick="setCheckboxes(\'usersForm\', false); return false;">' . $GLOBALS['strUncheckAll'] . '</a> )' . "\n"
  475.            . $spaces . '    </legend>' . "\n"
  476.            . $spaces . '    <p><small><i>' . $GLOBALS['strEnglishPrivileges'] . '</i></small></p>' . "\n"
  477.            . $spaces . '    <fieldset>' . "\n"
  478.            . $spaces . '        <legend>' . $GLOBALS['strData'] . '</legend>' . "\n";
  479.         foreach ( $privTable[0] as $priv )
  480.         {
  481.             echo $spaces . '        <div class="item">' . "\n"
  482.                . $spaces . '            <input type="checkbox"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
  483.                . $spaces . '            <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
  484.                . $spaces . '        </div>' . "\n";
  485.         }
  486.         echo $spaces . '    </fieldset>' . "\n"
  487.            . $spaces . '    <fieldset>' . "\n"
  488.            . $spaces . '        <legend>' . $GLOBALS['strStructure'] . '</legend>' . "\n";
  489.         foreach ( $privTable[1] as $priv )
  490.         {
  491.             echo $spaces . '        <div class="item">' . "\n"
  492.                . $spaces . '            <input type="checkbox"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
  493.                . $spaces . '            <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
  494.                . $spaces . '        </div>' . "\n";
  495.         }
  496.         echo $spaces . '    </fieldset>' . "\n"
  497.            . $spaces . '    <fieldset>' . "\n"
  498.            . $spaces . '        <legend>' . $GLOBALS['strAdministration'] . '</legend>' . "\n";
  499.         foreach ( $privTable[2] as $priv )
  500.         {
  501.             echo $spaces . '        <div class="item">' . "\n"
  502.                . $spaces . '            <input type="checkbox"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' name="' . $priv[0] . '_priv" id="checkbox_' . $priv[0] . '_priv" value="Y" ' . ($row[$priv[0] . '_priv'] == 'Y' ? 'checked="checked" ' : '') . 'title="' . $priv[2] . '"/>' . "\n"
  503.                . $spaces . '            <label for="checkbox_' . $priv[0] . '_priv"><tt><dfn title="' . $priv[2] . '">' . $priv[1] . '</dfn></tt></label>' . "\n"
  504.                . $spaces . '        </div>' . "\n";
  505.         }
  506.  
  507.         echo $spaces . '    </fieldset>' . "\n";
  508.         // The "Resource limits" box is not displayed for db-specific privs
  509.         if ($db == '*' && PMA_MYSQL_INT_VERSION >= 40002) {
  510.             echo $spaces . '    <fieldset>' . "\n"
  511.                . $spaces . '        <legend>' . $GLOBALS['strResourceLimits'] . '</legend>' . "\n"
  512.                . $spaces . '        <p><small><i>' . $GLOBALS['strZeroRemovesTheLimit'] . '</i></small></p>' . "\n"
  513.                . $spaces . '        <div class="item">' . "\n"
  514.                . $spaces . '            <label for="text_max_questions"><tt><dfn title="' . $GLOBALS['strPrivDescMaxQuestions'] . '">MAX QUERIES PER HOUR</dfn></tt></label>' . "\n"
  515.                . $spaces . '            <input type="text" name="max_questions" id="text_max_questions" value="' . $row['max_questions'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxQuestions'] . '" />' . "\n"
  516.                . $spaces . '        </div>' . "\n"
  517.                . $spaces . '        <div class="item">' . "\n"
  518.                . $spaces . '            <label for="text_max_updates"><tt><dfn title="' . $GLOBALS['strPrivDescMaxUpdates'] . '">MAX UPDATES PER HOUR</dfn></tt></label>' . "\n"
  519.                . $spaces . '            <input type="text" name="max_updates" id="text_max_updates" value="' . $row['max_updates'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUpdates'] . '" />' . "\n"
  520.                . $spaces . '        </div>' . "\n"
  521.                . $spaces . '        <div class="item">' . "\n"
  522.                . $spaces . '            <label for="text_max_connections"><tt><dfn title="' . $GLOBALS['strPrivDescMaxConnections'] . '">MAX CONNECTIONS PER HOUR</dfn></tt></label>' . "\n"
  523.                . $spaces . '            <input type="text" name="max_connections" id="text_max_connections" value="' . $row['max_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxConnections'] . '" />' . "\n"
  524.                . $spaces . '        </div>' . "\n";
  525.  
  526.             if (PMA_MYSQL_INT_VERSION >= 50003) {
  527.                 echo $spaces . '        <div class="item">' . "\n"
  528.                    . $spaces . '            <label for="text_max_user_connections"><tt><dfn title="' . $GLOBALS['strPrivDescMaxUserConnections'] . '">MAX USER_CONNECTIONS</dfn></tt></label>' . "\n"
  529.                    . $spaces . '            <input type="text" name="max_user_connections" id="text_max_user_connections" value="' . $row['max_user_connections'] . '" size="11" maxlength="11" title="' . $GLOBALS['strPrivDescMaxUserConnections'] . '" />' . "\n"
  530.                    . $spaces . '        </div>' . "\n";
  531.             }
  532.             echo $spaces . '    </fieldset>' . "\n";
  533.            }
  534.         // for Safari 2.0.2
  535.         echo $spaces . '    <div class="clearfloat"></div>' . "\n";
  536.     }
  537.     echo $spaces . '</fieldset>' . "\n";
  538.     if ($submit) {
  539.         echo $spaces . '<fieldset id="fieldset_user_privtable_footer" class="tblFooters">' . "\n"
  540.            . $spaces . '    <input type="submit" name="update_privs" value="' . $GLOBALS['strGo'] . '" />' . "\n"
  541.            . $spaces . '</fieldset>' . "\n";
  542.     }
  543. } // end of the 'PMA_displayPrivTable()' function
  544.  
  545.  
  546. /**
  547.  * Displays the fields used by the "new user" form as well as the
  548.  * "change login information / copy user" form.
  549.  *
  550.  * @param   string     $mode    are we creating a new user or are we just
  551.  *                              changing  one? (allowed values: 'new', 'change')
  552.  * @param   int        $indent  the indenting level of the code
  553.  *
  554.  * @global  array      $cfg     the phpMyAdmin configuration
  555.  * @global  ressource  $user_link the database connection
  556.  *
  557.  * @return  void
  558.  */
  559. function PMA_displayLoginInformationFields($mode = 'new', $indent = 0 ) {
  560.     $spaces = str_repeat( '    ', $indent);
  561.  
  562.     // Get user/host name lengths
  563.     $fields_info = PMA_DBI_get_fields('mysql', 'user');
  564.     $username_length = 16;
  565.     $hostname_length = 41;
  566.     foreach ($fields_info as $key => $val) {
  567.         if ($val['Field'] == 'User') {
  568.             strtok($val['Type'], '()');
  569.             $v = strtok('()');
  570.             if (is_int($v)) {
  571.                 $username_length = $v;
  572.             }
  573.         } elseif ($val['Field'] == 'Host') {
  574.             strtok($val['Type'], '()');
  575.             $v = strtok('()');
  576.             if (is_int($v)) {
  577.                 $hostname_length = $v;
  578.             }
  579.         }
  580.     }
  581.     unset($fields_info);
  582.  
  583.     if ( isset( $GLOBALS['username'] ) && strlen( $GLOBALS['username'] ) === 0 ) {
  584.         $GLOBALS['pred_username'] = 'any';
  585.     }
  586.     echo $spaces . '<fieldset id="fieldset_add_user_login">' . "\n"
  587.        . $spaces . '<legend>' . $GLOBALS['strLoginInformation'] . '</legend>' . "\n"
  588.        . $spaces . '<div class="item">' . "\n"
  589.        . $spaces . '<label for="select_pred_username">' . "\n"
  590.        . $spaces . '    ' . $GLOBALS['strUserName'] . ':' . "\n"
  591.        . $spaces . '</label>' . "\n"
  592.        . $spaces . '<span class="options">' . "\n"
  593.        . $spaces . '    <select name="pred_username" id="select_pred_username" title="' . $GLOBALS['strUserName'] . '"' . "\n"
  594.        . $spaces . '        onchange="if (this.value == \'any\') { username.value = \'\'; } else if (this.value == \'userdefined\') { username.focus(); username.select(); }">' . "\n"
  595.        . $spaces . '        <option value="any"' . ((isset($GLOBALS['pred_username']) && $GLOBALS['pred_username'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyUser'] . '</option>' . "\n"
  596.        . $spaces . '        <option value="userdefined"' . ((!isset($GLOBALS['pred_username']) || $GLOBALS['pred_username'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
  597.        . $spaces . '    </select>' . "\n"
  598.        . $spaces . '</span>' . "\n"
  599.        . $spaces . '<input type="text" name="username" maxlength="' . $username_length . '" title="' . $GLOBALS['strUserName'] . '"' . (empty($GLOBALS['username']) ? '' : ' value="' . (isset($GLOBALS['new_username']) ? $GLOBALS['new_username'] : $GLOBALS['username']) . '"') . ' onchange="pred_username.value = \'userdefined\';" />' . "\n"
  600.        . $spaces . '</div>' . "\n"
  601.        . $spaces . '<div class="item">' . "\n"
  602.        . $spaces . '<label for="select_pred_hostname">' . "\n"
  603.        . $spaces . '    ' . $GLOBALS['strHost'] . ':' . "\n"
  604.        . $spaces . '</label>' . "\n"
  605.        . $spaces . '<span class="options">' . "\n"
  606.        . $spaces . '    <select name="pred_hostname" id="select_pred_hostname" title="' . $GLOBALS['strHost'] . '"' . "\n";
  607.     $res = PMA_DBI_query('SELECT USER();');
  608.     $row = PMA_DBI_fetch_row($res);
  609.     PMA_DBI_free_result($res);
  610.     unset($res);
  611.     if (!empty($row[0])) {
  612.         $thishost = str_replace("'", '', substr($row[0], (strrpos($row[0], '@') + 1)));
  613.         if ($thishost == 'localhost' || $thishost == '127.0.0.1') {
  614.             unset($thishost);
  615.         }
  616.     }
  617.     echo $spaces . '    onchange="if (this.value == \'any\') { hostname.value = \'%\'; } else if (this.value == \'localhost\') { hostname.value = \'localhost\'; } '
  618.        . (empty($thishost) ? '' : 'else if (this.value == \'thishost\') { hostname.value = \'' . addslashes(htmlspecialchars($thishost)) . '\'; } ')
  619.        . 'else if (this.value == \'hosttable\') { hostname.value = \'\'; } else if (this.value == \'userdefined\') { hostname.focus(); hostname.select(); }">' . "\n";
  620.     unset($row);
  621.  
  622.     // when we start editing a user, $GLOBALS['pred_hostname'] is not defined
  623.     if (!isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) {
  624.         switch (strtolower($GLOBALS['hostname'])) {
  625.             case 'localhost':
  626.             case '127.0.0.1':
  627.                 $GLOBALS['pred_hostname'] = 'localhost';
  628.                 break;
  629.             case '%':
  630.                 $GLOBALS['pred_hostname'] = 'any';
  631.                 break;
  632.             default:
  633.                 $GLOBALS['pred_hostname'] = 'userdefined';
  634.                 break;
  635.         }
  636.     }
  637.     echo $spaces . '        <option value="any"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'any') ? ' selected="selected"' : '') . '>' . $GLOBALS['strAnyHost'] . '</option>' . "\n"
  638.        . $spaces . '        <option value="localhost"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'localhost') ? ' selected="selected"' : '') . '>' . $GLOBALS['strLocalhost'] . '</option>' . "\n";
  639.     if (!empty($thishost)) {
  640.         echo $spaces . '        <option value="thishost"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'thishost') ? ' selected="selected"' : '') . '>' . $GLOBALS['strThisHost'] . '</option>' . "\n";
  641.     }
  642.     unset($thishost);
  643.     echo $spaces . '        <option value="hosttable"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'hosttable') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseHostTable'] . '</option>' . "\n"
  644.        . $spaces . '        <option value="userdefined"' . ((isset($GLOBALS['pred_hostname']) && $GLOBALS['pred_hostname'] == 'userdefined') ? ' selected="selected"' : '') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
  645.        . $spaces . '    </select>' . "\n"
  646.        . $spaces . '</span>' . "\n"
  647.        . $spaces . '<input type="text" name="hostname" maxlength="' . $hostname_length . '" value="' . ( isset($GLOBALS['hostname']) ? $GLOBALS['hostname'] : '' ) . '" title="' . $GLOBALS['strHost'] . '" onchange="pred_hostname.value = \'userdefined\';" />' . "\n"
  648.        . $spaces . '</div>' . "\n"
  649.        . $spaces . '<div class="item">' . "\n"
  650.        . $spaces . '<label for="select_pred_password">' . "\n"
  651.        . $spaces . '    ' . $GLOBALS['strPassword'] . ':' . "\n"
  652.        . $spaces . '</label>' . "\n"
  653.        . $spaces . '<span class="options">' . "\n"
  654.        . $spaces . '    <select name="pred_password" id="select_pred_password" title="' . $GLOBALS['strPassword'] . '"' . "\n"
  655.        . $spaces . '            onchange="if (this.value == \'none\') { pma_pw.value = \'\'; pma_pw2.value = \'\'; } else if (this.value == \'userdefined\') { pma_pw.focus(); pma_pw.select(); }">' . "\n"
  656.        . ($mode == 'change' ? $spaces . '            <option value="keep" selected="selected">' . $GLOBALS['strKeepPass'] . '</option>' . "\n" : '')
  657.        . $spaces . '        <option value="none"';
  658.     if ( isset( $GLOBALS['username'] ) && $mode != 'change' ) {
  659.         echo '  selected="selected"';
  660.     }
  661.     echo $spaces . '>' . $GLOBALS['strNoPassword'] . '</option>' . "\n"
  662.        . $spaces . '        <option value="userdefined"' . ( isset( $GLOBALS['username'] ) ? '' : ' selected="selected"') . '>' . $GLOBALS['strUseTextField'] . ':</option>' . "\n"
  663.        . $spaces . '    </select>' . "\n"
  664.        . $spaces . '</span>' . "\n"
  665.        . $spaces . '<input type="password" id="text_pma_pw" name="pma_pw" title="' . $GLOBALS['strPassword'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
  666.        . $spaces . '</div>' . "\n"
  667.        . $spaces . '<div class="item">' . "\n"
  668.        . $spaces . '<label for="text_pma_pw2">' . "\n"
  669.        . $spaces . '    ' . $GLOBALS['strReType'] . ':' . "\n"
  670.        . $spaces . '</label>' . "\n"
  671.        . $spaces . '<span class="options"> </span>' . "\n"
  672.        . $spaces . '<input type="password" name="pma_pw2" id="text_pma_pw2" title="' . $GLOBALS['strReType'] . '" onchange="pred_password.value = \'userdefined\';" />' . "\n"
  673.        . $spaces . '</div>' . "\n"
  674.        . $spaces . '<div class="item">' . "\n"
  675.        . $spaces . '<label for="button_generate_password">' . "\n"
  676.        . $spaces . '    ' . $GLOBALS['strGeneratePassword'] . ':' . "\n"
  677.        . $spaces . '</label>' . "\n"
  678.        . $spaces . '<span class="options">' . "\n"
  679.        . $spaces . '    <input type="button" id="button_generate_password" value="' . $GLOBALS['strGenerate'] . '" onclick="suggestPassword()" />' . "\n"
  680.        . $spaces . '    <input type="button" id="button_copy_password" value="' . $GLOBALS['strCopy'] . '" onclick="suggestPasswordCopy(this.form)" />' . "\n"
  681.        . $spaces . '</span>' . "\n"
  682.        . $spaces . '<input type="text" name="generated_pw" id="generated_pw" />' . "\n"
  683.        . $spaces . '</div>' . "\n"
  684.        . $spaces . '</fieldset>' . "\n";
  685. } // end of the 'PMA_displayUserAndHostFields()' function
  686.  
  687.  
  688. /**
  689.  * Changes / copies a user, part I
  690.  */
  691. if (!empty($change_copy)) {
  692.     $user_host_condition =
  693.         ' WHERE ' . PMA_convert_using('User')
  694.         .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
  695.         .' AND ' . PMA_convert_using('Host')
  696.         .' = ' . PMA_convert_using($old_hostname, 'quoted') . ';';
  697.     $res = PMA_DBI_query('SELECT * FROM `mysql`.`user` ' . $user_host_condition);
  698.     if (!$res) {
  699.         $message = $GLOBALS['strNoUsersFound'];
  700.         unset($change_copy);
  701.     } else {
  702.         $row = PMA_DBI_fetch_assoc($res);
  703.         extract($row, EXTR_OVERWRITE);
  704.         // Recent MySQL versions have the field "Password" in mysql.user,
  705.         // so the previous extract creates $Password but this script
  706.         // uses $password
  707.         if (!isset($password) && isset($Password)) {
  708.             $password=$Password;
  709.         }
  710.         PMA_DBI_free_result($res);
  711.         $queries = array();
  712.     }
  713. }
  714.  
  715.  
  716. /**
  717.  * Adds a user
  718.  *   (Changes / copies a user, part II)
  719.  */
  720. if (!empty($adduser_submit) || !empty($change_copy)) {
  721.     unset($sql_query);
  722.     if ($pred_username == 'any') {
  723.         $username = '';
  724.     }
  725.     switch ($pred_hostname) {
  726.         case 'any':
  727.             $hostname = '%';
  728.             break;
  729.         case 'localhost':
  730.             $hostname = 'localhost';
  731.             break;
  732.         case 'hosttable':
  733.             $hostname = '';
  734.             break;
  735.         case 'thishost':
  736.             $res = PMA_DBI_query('SELECT USER();');
  737.             $row = PMA_DBI_fetch_row($res);
  738.             PMA_DBI_free_result($res);
  739.             unset($res);
  740.             $hostname = substr($row[0], (strrpos($row[0], '@') + 1));
  741.             unset($row);
  742.             break;
  743.     }
  744.     $res = PMA_DBI_query(
  745.         'SELECT \'foo\' FROM `mysql`.`user`'
  746.         .' WHERE ' . PMA_convert_using('User')
  747.         .' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  748.         .' AND ' . PMA_convert_using('Host')
  749.         .' = ' . PMA_convert_using($hostname, 'quoted') . ';',
  750.         null, PMA_DBI_QUERY_STORE);
  751.     if (PMA_DBI_num_rows($res) == 1) {
  752.         PMA_DBI_free_result($res);
  753.         $message = sprintf($GLOBALS['strUserAlreadyExists'], '[i]\'' . $username . '\'@\'' . $hostname . '\'[/i]');
  754.         $adduser = 1;
  755.     } else {
  756.         PMA_DBI_free_result($res);
  757.  
  758.         if (50002 <= PMA_MYSQL_INT_VERSION) {
  759.             // MySQL 5 requires CREATE USER before any GRANT on this user can done
  760.             $create_user_real = 'CREATE USER \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
  761.         }
  762.  
  763.         $real_sql_query =
  764.             'GRANT ' . join(', ', PMA_extractPrivInfo()) . ' ON *.* TO \''
  765.             . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
  766.         if ($pred_password != 'none' && $pred_password != 'keep') {
  767.             $pma_pw_hidden = str_repeat('*', strlen($pma_pw));
  768.             $sql_query = $real_sql_query . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
  769.             $real_sql_query .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
  770.             if ( isset( $create_user_real ) ) {
  771.                 $create_user_show = $create_user_real . ' IDENTIFIED BY \'' . $pma_pw_hidden . '\'';
  772.                 $create_user_real .= ' IDENTIFIED BY \'' . PMA_sqlAddslashes($pma_pw) . '\'';
  773.             }
  774.         } else {
  775.             if ($pred_password == 'keep' && !empty($password)) {
  776.                 $real_sql_query .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
  777.                 if ( isset( $create_user_real ) ) {
  778.                     $create_user_real .= ' IDENTIFIED BY PASSWORD \'' . $password . '\'';
  779.                 }
  780.             }
  781.             $sql_query = $real_sql_query;
  782.             if ( isset( $create_user_real ) ) {
  783.                 $create_user_show = $create_user_real;
  784.             }
  785.         }
  786.         // FIXME: similar code appears twice in this script
  787.         if ((isset($Grant_priv) && $Grant_priv == 'Y') || (PMA_MYSQL_INT_VERSION >= 40002 && (isset($max_questions) || isset($max_connections) || isset($max_updates) || isset($max_user_connections)))) {
  788.             $real_sql_query .= 'WITH';
  789.             $sql_query .= 'WITH';
  790.             if (isset($Grant_priv) && $Grant_priv == 'Y') {
  791.                 $real_sql_query .= ' GRANT OPTION';
  792.                 $sql_query .= ' GRANT OPTION';
  793.             }
  794.             if (PMA_MYSQL_INT_VERSION >= 40002) {
  795.                 if (isset($max_questions)) {
  796.                     // avoid negative values
  797.                     $max_questions = max(0, (int)$max_questions);
  798.                     $real_sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
  799.                     $sql_query .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
  800.                 }
  801.                 if (isset($max_connections)) {
  802.                     $max_connections = max(0, (int)$max_connections);
  803.                     $real_sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
  804.                     $sql_query .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
  805.                 }
  806.                 if (isset($max_updates)) {
  807.                     $max_updates = max(0, (int)$max_updates);
  808.                     $real_sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
  809.                     $sql_query .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
  810.                 }
  811.             }
  812.             if (PMA_MYSQL_INT_VERSION >= 50003) {
  813.                 if (isset($max_user_connections)) {
  814.                     $max_user_connections = max(0, (int)$max_user_connections);
  815.                     $real_sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
  816.                     $sql_query .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
  817.                 }
  818.             }
  819.         }
  820.         if ( isset( $create_user_real ) ) {
  821.             $create_user_real .= ';';
  822.             $create_user_show .= ';';
  823.         }
  824.         $real_sql_query .= ';';
  825.         $sql_query .= ';';
  826.         if (empty($change_copy)) {
  827.             if ( isset( $create_user_real ) ) {
  828.                 PMA_DBI_try_query($create_user_real) or PMA_mysqlDie(PMA_DBI_getError(), $create_user_show);
  829.                 $sql_query = $create_user_show . $sql_query;
  830.             }
  831.             PMA_DBI_try_query($real_sql_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
  832.             $message = $GLOBALS['strAddUserMessage'];
  833.  
  834.             /* Create database for new user */
  835.             if (isset($createdb) && $createdb > 0) {
  836.                 if ($createdb == 1) {
  837.                     $q = 'CREATE DATABASE ' . PMA_backquote(PMA_sqlAddslashes($username)) . ';';
  838.                     $sql_query .= $q;
  839.                     PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
  840.  
  841.                     $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username)) . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
  842.                     $sql_query .= $q;
  843.                     PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
  844.                 } elseif ($createdb == 2) {
  845.                     $q = 'GRANT ALL PRIVILEGES ON ' . PMA_backquote(PMA_sqlAddslashes($username) . '_%') . '.* TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
  846.                     $sql_query .= $q;
  847.                     PMA_DBI_try_query($q) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
  848.                 }
  849.             }
  850.         } else {
  851.             if ( isset( $create_user_real ) ) {
  852.                 $queries[]             = $create_user_real;
  853.             }
  854.             $queries[]             = $real_sql_query;
  855.             // we put the query containing the hidden password in
  856.             // $queries_for_display, at the same position occupied
  857.             // by the real query in $queries
  858.             $tmp_count = count($queries);
  859.             if ( isset( $create_user_real ) ) {
  860.                 $queries_for_display[$tmp_count - 2] = $create_user_show;
  861.             }
  862.             $queries_for_display[$tmp_count - 1] = $sql_query;
  863.         }
  864.         unset($res, $real_sql_query);
  865.     }
  866. }
  867.  
  868.  
  869. /**
  870.  * Changes / copies a user, part III
  871.  */
  872. if (!empty($change_copy)) {
  873.     $user_host_condition =
  874.         ' WHERE ' . PMA_convert_using('User')
  875.         .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
  876.         .' AND ' . PMA_convert_using('Host')
  877.         .' = ' . PMA_convert_using($old_hostname, 'quoted') . ';';
  878.     $res = PMA_DBI_query('SELECT * FROM `mysql`.`db`' . $user_host_condition );
  879.     while ($row = PMA_DBI_fetch_assoc($res)) {
  880.         $queries[] =
  881.             'GRANT ' . join(', ', PMA_extractPrivInfo($row))
  882.             .' ON `' . $row['Db'] . '`.*'
  883.             .' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
  884.             . ( $row['Grant_priv'] == 'Y' ? ' WITH GRANT OPTION;' : ';' );
  885.     }
  886.     PMA_DBI_free_result($res);
  887.     $res = PMA_DBI_query(
  888.         'SELECT `Db`, `Table_name`, `Table_priv`'
  889.         .' FROM `mysql`.`tables_priv`' . $user_host_condition,
  890.         $GLOBALS['userlink'], PMA_DBI_QUERY_STORE );
  891.     while ($row = PMA_DBI_fetch_assoc($res)) {
  892.  
  893.         $res2 = PMA_DBI_QUERY(
  894.             'SELECT `Column_name`, `Column_priv`'
  895.             .' FROM `mysql`.`columns_priv`'
  896.             .' WHERE ' . PMA_convert_using('User')
  897.             .' = ' . PMA_convert_using(PMA_sqlAddslashes($old_username), 'quoted')
  898.             .' AND ' . PMA_convert_using('`Host`')
  899.             .' = ' . PMA_convert_using($old_hostname, 'quoted')
  900.             .' AND ' . PMA_convert_using('`Db`')
  901.             .' = ' . PMA_convert_using($row['Db'], 'quoted')
  902.             .' AND ' . PMA_convert_using('`Table_name`')
  903.             .' = ' . PMA_convert_using($row['Table_name'], 'quoted')
  904.             .';',
  905.             null, PMA_DBI_QUERY_STORE);
  906.  
  907.         $tmp_privs1 = PMA_extractPrivInfo($row);
  908.         $tmp_privs2 = array(
  909.             'Select' => array(),
  910.             'Insert' => array(),
  911.             'Update' => array(),
  912.             'References' => array()
  913.         );
  914.  
  915.         while ($row2 = PMA_DBI_fetch_assoc($res2)) {
  916.             $tmp_array = explode(',', $row2['Column_priv']);
  917.             if (in_array('Select', $tmp_array)) {
  918.                 $tmp_privs2['Select'][] = $row2['Column_name'];
  919.             }
  920.             if (in_array('Insert', $tmp_array)) {
  921.                 $tmp_privs2['Insert'][] = $row2['Column_name'];
  922.             }
  923.             if (in_array('Update', $tmp_array)) {
  924.                 $tmp_privs2['Update'][] = $row2['Column_name'];
  925.             }
  926.             if (in_array('References', $tmp_array)) {
  927.                 $tmp_privs2['References'][] = $row2['Column_name'];
  928.             }
  929.             unset($tmp_array);
  930.         }
  931.         if (count($tmp_privs2['Select']) > 0 && !in_array('SELECT', $tmp_privs1)) {
  932.             $tmp_privs1[] = 'SELECT (`' . join('`, `', $tmp_privs2['Select']) . '`)';
  933.         }
  934.         if (count($tmp_privs2['Insert']) > 0 && !in_array('INSERT', $tmp_privs1)) {
  935.             $tmp_privs1[] = 'INSERT (`' . join('`, `', $tmp_privs2['Insert']) . '`)';
  936.         }
  937.         if (count($tmp_privs2['Update']) > 0 && !in_array('UPDATE', $tmp_privs1)) {
  938.             $tmp_privs1[] = 'UPDATE (`' . join('`, `', $tmp_privs2['Update']) . '`)';
  939.         }
  940.         if (count($tmp_privs2['References']) > 0 && !in_array('REFERENCES', $tmp_privs1)) {
  941.             $tmp_privs1[] = 'REFERENCES (`' . join('`, `', $tmp_privs2['References']) . '`)';
  942.         }
  943.         unset($tmp_privs2);
  944.         $queries[] =
  945.             'GRANT ' . join(', ', $tmp_privs1)
  946.             . ' ON `' . $row['Db'] . '`.`' . $row['Table_name']
  947.             . '` TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\''
  948.             . (in_array('Grant', explode(',', $row['Table_priv'])) ? ' WITH GRANT OPTION;' : ';');
  949.     }
  950. }
  951.  
  952.  
  953. /**
  954.  * Updates privileges
  955.  */
  956. if (!empty($update_privs)) {
  957.     // escaping a wildcard character in a GRANT is only accepted at the global
  958.     // or database level, not at table level; this is why I remove
  959.     // the escaping character
  960.     // Note: in the phpMyAdmin list of Database-specific privileges,
  961.     //  we will have for example
  962.     //  test\_db  SELECT (this one is for privileges on a db level)
  963.     //  test_db   USAGE  (this one is for table-specific privileges)
  964.     //
  965.     // It looks curious but reflects the way MySQL works
  966.  
  967.     if (! isset($dbname) || ! strlen($dbname)) {
  968.         $db_and_table = '*.*';
  969.     } else {
  970.         if ( isset( $tablename ) && strlen($tablename) ) {
  971.             $db_and_table = PMA_backquote( PMA_unescape_mysql_wildcards( $dbname ) ) . '.';
  972.             $db_and_table .= PMA_backquote( $tablename );
  973.         } else {
  974.             $db_and_table = PMA_backquote( $dbname ) . '.';
  975.             $db_and_table .= '*';
  976.         }
  977.     }
  978.  
  979.     $sql_query0 =
  980.         'REVOKE ALL PRIVILEGES ON ' . $db_and_table
  981.         . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
  982.     if (!isset($Grant_priv) || $Grant_priv != 'Y') {
  983.         $sql_query1 =
  984.             'REVOKE GRANT OPTION ON ' . $db_and_table
  985.             . ' FROM \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\';';
  986.     }
  987.     $sql_query2 =
  988.         'GRANT ' . join(', ', PMA_extractPrivInfo())
  989.         . ' ON ' . $db_and_table
  990.         . ' TO \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\'';
  991.  
  992.         // FIXME: similar code appears twice in this script
  993.     if ( ( isset($Grant_priv) && $Grant_priv == 'Y')
  994.       || ( ( ! isset($dbname) || ! strlen($dbname) ) && PMA_MYSQL_INT_VERSION >= 40002
  995.         && ( isset($max_questions) || isset($max_connections)
  996.           || isset($max_updates) || isset($max_user_connections))))
  997.     {
  998.         $sql_query2 .= 'WITH';
  999.         if (isset($Grant_priv) && $Grant_priv == 'Y') {
  1000.             $sql_query2 .= ' GRANT OPTION';
  1001.         }
  1002.         if (PMA_MYSQL_INT_VERSION >= 40002) {
  1003.             if (isset($max_questions)) {
  1004.                 $max_questions = max(0, (int)$max_questions);
  1005.                 $sql_query2 .= ' MAX_QUERIES_PER_HOUR ' . $max_questions;
  1006.             }
  1007.             if (isset($max_connections)) {
  1008.                 $max_connections = max(0, (int)$max_connections);
  1009.                 $sql_query2 .= ' MAX_CONNECTIONS_PER_HOUR ' . $max_connections;
  1010.             }
  1011.             if (isset($max_updates)) {
  1012.                 $max_updates = max(0, (int)$max_updates);
  1013.                 $sql_query2 .= ' MAX_UPDATES_PER_HOUR ' . $max_updates;
  1014.             }
  1015.         }
  1016.         if (PMA_MYSQL_INT_VERSION >= 50003) {
  1017.             if (isset($max_user_connections)) {
  1018.                 $max_user_connections = max(0, (int)$max_user_connections);
  1019.                 $sql_query2 .= ' MAX_USER_CONNECTIONS ' . $max_user_connections;
  1020.             }
  1021.         }
  1022.     }
  1023.     $sql_query2 .= ';';
  1024.     if (!PMA_DBI_try_query($sql_query0)) { // this query may fail, but this does not matter :o)
  1025.         // a case when it can fail is when the admin does not have all
  1026.         // privileges: he can't do a REVOKE ALL PRIVILEGES !
  1027.         // so at least we display the error
  1028.         echo PMA_DBI_getError();
  1029.         unset($sql_query0);
  1030.     }
  1031.     if (isset($sql_query1) && !PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
  1032.         unset($sql_query1);
  1033.     }
  1034.     PMA_DBI_query($sql_query2);
  1035.     $sql_query = (isset($sql_query0) ? $sql_query0 . ' ' : '')
  1036.                . (isset($sql_query1) ? $sql_query1 . ' ' : '')
  1037.                . $sql_query2;
  1038.     $message = sprintf($GLOBALS['strUpdatePrivMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
  1039. }
  1040.  
  1041.  
  1042. /**
  1043.  * Revokes Privileges
  1044.  */
  1045. if (!empty($revokeall)) {
  1046.  
  1047.     if ( ! isset($dbname) || ! strlen($dbname) ) {
  1048.         $db_and_table = '*.*';
  1049.     } else {
  1050.         if ( ! isset( $tablename ) || ! strlen($tablename) ) {
  1051.             $db_and_table = PMA_backquote( $dbname ) . '.';
  1052.             $db_and_table .= '*';
  1053.         } else {
  1054.             $db_and_table = PMA_backquote( PMA_unescape_mysql_wildcards( $dbname ) ) . '.';
  1055.             $db_and_table .= PMA_backquote( $tablename );
  1056.         }
  1057.     }
  1058.  
  1059.     $sql_query0 =
  1060.         'REVOKE ALL PRIVILEGES ON ' . $db_and_table
  1061.         . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
  1062.     $sql_query1 =
  1063.         'REVOKE GRANT OPTION ON ' . $db_and_table
  1064.         . ' FROM \'' . $username . '\'@\'' . $hostname . '\';';
  1065.     PMA_DBI_query($sql_query0);
  1066.     if (!PMA_DBI_try_query($sql_query1)) { // this one may fail, too...
  1067.         unset($sql_query1);
  1068.     }
  1069.     $sql_query = $sql_query0 . (isset($sql_query1) ? ' ' . $sql_query1 : '');
  1070.     $message = sprintf($GLOBALS['strRevokeMessage'], '\'' . $username . '\'@\'' . $hostname . '\'');
  1071.     if ( ! isset($tablename) || ! strlen($tablename) ) {
  1072.         unset($dbname);
  1073.     } else {
  1074.         unset($tablename);
  1075.     }
  1076. }
  1077.  
  1078.  
  1079. /**
  1080.  * Updates the password
  1081.  */
  1082. if (!empty($change_pw)) {
  1083.     if ($nopass == 1) {
  1084.         $sql_query = 'SET PASSWORD FOR \'' . $username . '\'@\'' . $hostname . '\' = \'\';';
  1085.         PMA_DBI_query($sql_query);
  1086.         $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
  1087.     } elseif (empty($pma_pw) || empty($pma_pw2)) {
  1088.         $message = $GLOBALS['strPasswordEmpty'];
  1089.     } elseif ($pma_pw != $pma_pw2) {
  1090.         $message = $GLOBALS['strPasswordNotSame'];
  1091.     } else {
  1092.         $hidden_pw = '';
  1093.         for ($i = 0; $i < strlen($pma_pw); $i++) {
  1094.             $hidden_pw .= '*';
  1095.         }
  1096.         $local_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = PASSWORD(\'' . PMA_sqlAddslashes($pma_pw) . '\')';
  1097.         $sql_query = 'SET PASSWORD FOR \'' . PMA_sqlAddslashes($username) . '\'@\'' . $hostname . '\' = PASSWORD(\'' . $hidden_pw . '\')';
  1098.         PMA_DBI_try_query($local_query) or PMA_mysqlDie(PMA_DBI_getError(), $sql_query);
  1099.         $message = sprintf($GLOBALS['strPasswordChanged'], '\'' . $username . '\'@\'' . $hostname . '\'');
  1100.     }
  1101. }
  1102.  
  1103.  
  1104. /**
  1105.  * Deletes users
  1106.  *   (Changes / copies a user, part IV)
  1107.  */
  1108. $user_host_separator = chr(27);
  1109.  
  1110. if (!empty($delete) || (!empty($change_copy) && $mode < 4)) {
  1111.     if (!empty($change_copy)) {
  1112.         $selected_usr = array($old_username . $user_host_separator . $old_hostname);
  1113.     } else {
  1114.         $queries = array();
  1115.     }
  1116.     for ($i = 0; isset($selected_usr[$i]); $i++) {
  1117.         list($this_user, $this_host) = explode($user_host_separator, $selected_usr[$i]);
  1118.         $queries[] = '# ' . sprintf($GLOBALS['strDeleting'], '\'' . $this_user . '\'@\'' . $this_host . '\'') . ' ...';
  1119.         if ( PMA_MYSQL_INT_VERSION >= 50002 ) {
  1120.             $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
  1121.         } else {
  1122.             if ($mode == 2) {
  1123.                 // The SHOW GRANTS query may fail if the user has not been loaded
  1124.                 // into memory
  1125.                 $res = PMA_DBI_try_query('SHOW GRANTS FOR \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';');
  1126.                 if ($res) {
  1127.                     $queries[] = 'REVOKE ALL PRIVILEGES ON *.* FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
  1128.                     while ($row = PMA_DBI_fetch_row($res)) {
  1129.                         $this_table = substr($row[0], (strpos($row[0], 'ON') + 3), (strpos($row[0], ' TO ') - strpos($row[0], 'ON') - 3));
  1130.                         if ($this_table != '*.*') {
  1131.                             $queries[] = 'REVOKE ALL PRIVILEGES ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
  1132.  
  1133.                             if (strpos($row[0], 'WITH GRANT OPTION')) {
  1134.                                 $queries[] = 'REVOKE GRANT OPTION ON ' . $this_table . ' FROM \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
  1135.                             }
  1136.                         }
  1137.                         unset($this_table);
  1138.                     }
  1139.                     PMA_DBI_free_result($res);
  1140.                 }
  1141.                 unset($res);
  1142.             }
  1143.             if ( PMA_MYSQL_INT_VERSION >= 40101 ) {
  1144.                 $queries[] = 'DROP USER \'' . PMA_sqlAddslashes($this_user) . '\'@\'' . $this_host . '\';';
  1145.             } else {
  1146.                 $queries[] = 'DELETE FROM `mysql`.`user` WHERE ' . PMA_convert_using('User') . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted') . ' AND ' . PMA_convert_using('Host') . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
  1147.             }
  1148.             if ($mode != 2) {
  1149.                 // If we REVOKE the table grants, we should not need to modify the
  1150.                 // `mysql`.`db`, `mysql`.`tables_priv` and `mysql`.`columns_priv` tables manually...
  1151.                 $user_host_condition =
  1152.                     ' WHERE ' . PMA_convert_using('User')
  1153.                     . ' = ' . PMA_convert_using(PMA_sqlAddslashes($this_user), 'quoted')
  1154.                     . ' AND ' . PMA_convert_using('Host')
  1155.                     . ' = ' . PMA_convert_using($this_host, 'quoted') . ';';
  1156.                 $queries[] = 'DELETE FROM `mysql`.`db`' . $user_host_condition;
  1157.                 $queries[] = 'DELETE FROM `mysql`.`tables_priv`' . $user_host_condition;
  1158.                 $queries[] = 'DELETE FROM `mysql`.`columns_priv`' . $user_host_condition;
  1159.             }
  1160.         }
  1161.         if (!empty($drop_users_db)) {
  1162.             $queries[] = 'DROP DATABASE IF EXISTS ' . PMA_backquote($this_user) . ';';
  1163.             $GLOBALS['reload'] = TRUE;
  1164.             PMA_reloadNavigation();
  1165.         }
  1166.     }
  1167.     if (empty($change_copy)) {
  1168.         if (empty($queries)) {
  1169.             $show_error_header = TRUE;
  1170.             $message = $GLOBALS['strDeleteNoUsersSelected'];
  1171.         } else {
  1172.             if ($mode == 3) {
  1173.                 $queries[] = '# ' . $GLOBALS['strReloadingThePrivileges'] . ' ...';
  1174.                 $queries[] = 'FLUSH PRIVILEGES;';
  1175.             }
  1176.             foreach ($queries as $sql_query) {
  1177.                 if ($sql_query{0} != '#') {
  1178.                     PMA_DBI_query($sql_query, $GLOBALS['userlink']);
  1179.                 }
  1180.             }
  1181.             $sql_query = join("\n", $queries);
  1182.             $message = $GLOBALS['strUsersDeleted'];
  1183.         }
  1184.         unset($queries);
  1185.     }
  1186. }
  1187.  
  1188.  
  1189. /**
  1190.  * Changes / copies a user, part V
  1191.  */
  1192. if (!empty($change_copy)) {
  1193.     $tmp_count = 0;
  1194.     foreach ($queries as $sql_query) {
  1195.         if ($sql_query{0} != '#') {
  1196.             PMA_DBI_query($sql_query);
  1197.         }
  1198.         // when there is a query containing a hidden password, take it
  1199.         // instead of the real query sent
  1200.         if (isset($queries_for_display[$tmp_count])) {
  1201.             $queries[$tmp_count] = $queries_for_display[$tmp_count];
  1202.         }
  1203.         $tmp_count++;
  1204.     }
  1205.     $message = $GLOBALS['strSuccess'];
  1206.     $sql_query = join("\n", $queries);
  1207. }
  1208.  
  1209.  
  1210. /**
  1211.  * Reloads the privilege tables into memory
  1212.  */
  1213. if (!empty($flush_privileges)) {
  1214.     $sql_query = 'FLUSH PRIVILEGES;';
  1215.     PMA_DBI_query($sql_query);
  1216.     $message = $GLOBALS['strPrivilegesReloaded'];
  1217. }
  1218.  
  1219.  
  1220. /**
  1221.  * Displays the links
  1222.  */
  1223. if (isset($viewing_mode) && $viewing_mode == 'db') {
  1224.      $db = $checkprivs;
  1225.      $url_query .= '&goto=db_operations.php';
  1226.  
  1227.      // Gets the database structure
  1228.      $sub_part = '_structure';
  1229.      require('./libraries/db_details_db_info.inc.php');
  1230.      echo "\n";
  1231. } else {
  1232.     require('./libraries/server_links.inc.php');
  1233. }
  1234.  
  1235.  
  1236. /**
  1237.  * defines some standard links
  1238.  */
  1239. $link_edit = '<a href="server_privileges.php?' . $GLOBALS['url_query']
  1240.     .'&username=%s'
  1241.     .'&hostname=%s'
  1242.     .'&dbname=%s'
  1243.     .'&tablename=%s">';
  1244. if ( $GLOBALS['cfg']['PropertiesIconic'] ) {
  1245.     $link_edit .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usredit.png" width="16" height="16" alt="' . $GLOBALS['strEditPrivileges'] . '" title="' . $GLOBALS['strEditPrivileges'] . '" />';
  1246. } else {
  1247.     $link_edit .= $GLOBALS['strEditPrivileges'];
  1248. }
  1249. $link_edit .= '</a>';
  1250.  
  1251. $link_revoke = '<a href="server_privileges.php?' . $GLOBALS['url_query']
  1252.     .'&username=%s'
  1253.     .'&hostname=%s'
  1254.     .'&dbname=%s'
  1255.     .'&tablename=%s'
  1256.     .'&revokeall=1">';
  1257. if ( $GLOBALS['cfg']['PropertiesIconic'] ) {
  1258.     $link_revoke .= '<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_usrdrop.png" width="16" height="16" alt="' . $GLOBALS['strRevoke'] . '" title="' . $GLOBALS['strRevoke'] . '" />';
  1259. } else {
  1260.     $link_revoke .= $GLOBALS['strRevoke'];
  1261. }
  1262. $link_revoke .= '</a>';
  1263.  
  1264. /**
  1265.  * Displays the page
  1266.  */
  1267. if ( empty( $adduser ) && ( ! isset( $checkprivs ) || ! strlen($checkprivs) ) ) {
  1268.     if ( ! isset( $username ) ) {
  1269.         // No username is given --> display the overview
  1270.         echo '<h2>' . "\n"
  1271.            . ($GLOBALS['cfg']['MainPageIconic'] ? '<img class="icon" src="'. $GLOBALS['pmaThemeImage'] . 'b_usrlist.png" alt="" />' : '')
  1272.            . $GLOBALS['strUserOverview'] . "\n"
  1273.            . '</h2>' . "\n";
  1274.  
  1275.         $sql_query =
  1276.             'SELECT `User`,' .
  1277.             '       `Host`,' .
  1278.             '       IF(`Password` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . '\'\', \'N\', \'Y\') AS \'Password\',' .
  1279.             '       `Select_priv`,' .
  1280.             '       `Insert_priv`,' .
  1281.             '       `Update_priv`,' .
  1282.             '       `Delete_priv`,' .
  1283.             '       `Index_priv`,' .
  1284.             '       `Alter_priv`,' .
  1285.             '       `Create_priv`,' .
  1286.             '       `Drop_priv`,' .
  1287.             '       `Grant_priv`,' .
  1288.             '       `References_priv`,' .
  1289.             '       `Reload_priv`,' .
  1290.             '       `Shutdown_priv`,' .
  1291.             '       `Process_priv`,' .
  1292.             '       `File_priv`';
  1293.  
  1294.         if (PMA_MYSQL_INT_VERSION >= 40002) {
  1295.             $sql_query .= ', `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`';
  1296.         }
  1297.  
  1298.         if (PMA_MYSQL_INT_VERSION >= 50001) {
  1299.             $sql_query .= ', `Create_view_priv`, `Show_view_priv`';
  1300.         }
  1301.  
  1302.         if (PMA_MYSQL_INT_VERSION >= 50003) {
  1303.             $sql_query .= ', `Create_user_priv`, `Create_routine_priv`, `Alter_routine_priv`';
  1304.         }
  1305.  
  1306.         $sql_query .= '  FROM `mysql`.`user`';
  1307.  
  1308.         $sql_query .= (isset($initial) ? PMA_RangeOfUsers($initial) : '');
  1309.  
  1310.         $sql_query .= ' ORDER BY `User` ASC, `Host` ASC;';
  1311.         $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
  1312.  
  1313.         if (! $res) {
  1314.             // the query failed! This may have two reasons:
  1315.             // - the user does not have enough privileges
  1316.             // - the privilege tables use a structure of an earlier version.
  1317.             // so let's try a more simple query
  1318.  
  1319.             $sql_query = 'SELECT * FROM `mysql`.`user`';
  1320.             $res = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_STORE);
  1321.  
  1322.             if (!$res) {
  1323.                 echo '<i>' . $GLOBALS['strNoPrivileges'] . '</i>' . "\n";
  1324.                 PMA_DBI_free_result($res);
  1325.                 unset($res);
  1326.             } else {
  1327.                 // rabus: This message is hardcoded because I will replace it by
  1328.                 // a automatic repair feature soon.
  1329.                 echo '<div class="warning">' . "\n"
  1330.                    . '    Warning: Your privilege table structure seems to be older than this MySQL version!<br />' . "\n"
  1331.                    . '    Please run the script <tt>mysql_fix_privilege_tables</tt> that should be included in your MySQL server distribution to solve this problem!' . "\n"
  1332.                    . '</div><br />' . "\n";
  1333.             }
  1334.         } else {
  1335.  
  1336.             // we also want users not in table `user` but in other table
  1337.             $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
  1338.  
  1339.             $tables_to_search_for_users = array(
  1340.                 'user', 'db', 'tables_priv', 'columns_priv', 'procs_priv',
  1341.             );
  1342.  
  1343.             $db_rights_sqls = array();
  1344.             foreach ( $tables_to_search_for_users as $table_search_in ) {
  1345.                 if ( in_array( $table_search_in, $tables ) ) {
  1346.                     $db_rights_sqls[] = 'SELECT DISTINCT `User`, `Host` FROM `mysql`.`' . $table_search_in . '` ' . (isset($initial) ? PMA_RangeOfUsers($initial) : '');
  1347.                 }
  1348.             }
  1349.  
  1350.             $user_defaults = array(
  1351.                 'User'      => '',
  1352.                 'Host'      => '%',
  1353.                 'Password'  => '?',
  1354.                 'Grant_priv' => 'N',
  1355.                 'privs'     => array( 'USAGE' ),
  1356.             );
  1357.  
  1358.             // for all initials, even non A-Z
  1359.             $array_initials = array();
  1360.             // for the rights
  1361.             $db_rights = array();
  1362.  
  1363.             // do not use UNION DISTINCT, as it's not allowed before
  1364.             // MySQL 4.0.17, and because "it does nothing" (cf manual)
  1365.             if ( PMA_MYSQL_INT_VERSION >= 40000 ) {
  1366.                 $db_rights_sql = '(' . implode( ') UNION (', $db_rights_sqls ) . ')'
  1367.                     .' ORDER BY `User` ASC, `Host` ASC';
  1368.  
  1369.                 $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1370.  
  1371.                 while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1372.                     $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1373.                     $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
  1374.                         $db_rights_row;
  1375.                 }
  1376.             } else {
  1377.                 foreach ( $db_rights_sqls as $db_rights_sql ) {
  1378.                     $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1379.  
  1380.                     while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1381.                         $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1382.                         $db_rights[$db_rights_row['User']][$db_rights_row['Host']] =
  1383.                             $db_rights_row;
  1384.                     }
  1385.                 }
  1386.             }
  1387.             PMA_DBI_free_result( $db_rights_result );
  1388.             unset( $db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row );
  1389.             ksort( $db_rights );
  1390.  
  1391.             /**
  1392.              * Displays the initials
  1393.              */
  1394.  
  1395.             // initialize to FALSE the letters A-Z
  1396.             for ($letter_counter = 1; $letter_counter < 27; $letter_counter++) {
  1397.                 if ( ! isset( $array_initials[chr($letter_counter + 64)] ) ) {
  1398.                     $array_initials[chr($letter_counter + 64)] = FALSE;
  1399.                 }
  1400.             }
  1401.  
  1402.             $initials = PMA_DBI_try_query('SELECT DISTINCT UPPER(LEFT(' . PMA_convert_using('User') . ',1)) FROM `user` ORDER BY `User` ASC', null, PMA_DBI_QUERY_STORE);
  1403.             while (list($tmp_initial) = PMA_DBI_fetch_row($initials)) {
  1404.                 $array_initials[$tmp_initial] = TRUE;
  1405.             }
  1406.  
  1407.             // Display the initials, which can be any characters, not
  1408.             // just letters. For letters A-Z, we add the non-used letters
  1409.             // as greyed out.
  1410.  
  1411.             uksort( $array_initials, "strnatcasecmp" );
  1412.  
  1413.             echo '<table cellspacing="5"><tr>';
  1414.             foreach ($array_initials as $tmp_initial => $initial_was_found) {
  1415.                 if ($initial_was_found) {
  1416.                     echo '<td><a href="' . $PHP_SELF . '?' . $GLOBALS['url_query'] . '&initial=' . urlencode($tmp_initial) . '">' . $tmp_initial . '</a></td>' . "\n";
  1417.                 } else {
  1418.                     echo '<td>' . $tmp_initial . '</td>';
  1419.                 }
  1420.             }
  1421.             echo '<td><a href="' . $PHP_SELF . '?' . $GLOBALS['url_query'] . '&showall=1">[' . $GLOBALS['strShowAll'] . ']</a></td>' . "\n";
  1422.             echo '</tr></table>';
  1423.  
  1424.             /**
  1425.             * Display the user overview
  1426.             * (if less than 50 users, display them immediately)
  1427.             */
  1428.  
  1429.             if (isset($initial) || isset($showall) || PMA_DBI_num_rows($res) < 50) {
  1430.  
  1431.                 while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
  1432.                     $row['privs'] = PMA_extractPrivInfo( $row, true );
  1433.                     $db_rights[$row['User']][$row['Host']] = $row;
  1434.                 }
  1435.                 @PMA_DBI_free_result( $res );
  1436.                 unset( $res );
  1437.  
  1438.                 echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
  1439.                    . PMA_generate_common_hidden_inputs('', '', 1)
  1440.                    . '    <table id="tableuserrights" class="data">' . "\n"
  1441.                    . '    <thead>' . "\n"
  1442.                    . '        <tr><td></td>' . "\n"
  1443.                    . '            <th>' . $GLOBALS['strUser'] . '</th>' . "\n"
  1444.                    . '            <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
  1445.                    . '            <th>' . $GLOBALS['strPassword'] . '</th>' . "\n"
  1446.                    . '            <th>' . $GLOBALS['strGlobalPrivileges'] . ' '
  1447.                    . PMA_showHint(  $GLOBALS['strEnglishPrivileges'] ) . '</th>' . "\n"
  1448.                    . '            <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
  1449.                    . '            ' . ($GLOBALS['cfg']['PropertiesIconic'] ? '<td></td>' : '<th>' . $GLOBALS['strAction'] . '</th>') . "\n";
  1450.                 echo '        </tr>' . "\n";
  1451.                 echo '    </thead>' . "\n";
  1452.                 echo '    <tbody>' . "\n";
  1453.                 $odd_row = true;
  1454.                 $index_checkbox = -1;
  1455.                 foreach ( $db_rights as $user ) {
  1456.                     $index_checkbox++;
  1457.                     ksort( $user );
  1458.                     foreach ( $user as $host ) {
  1459.                         $index_checkbox++;
  1460.                         echo '        <tr class="' . ( $odd_row ? 'odd' : 'even' ) . '">' . "\n"
  1461.                            . '            <td><input type="checkbox" name="selected_usr[]" id="checkbox_sel_users_' . $index_checkbox . '" value="' . str_replace( chr(27), '', htmlentities($host['User'] . $user_host_separator . $host['Host'] ) ) . '"' . (empty($GLOBALS['checkall']) ?  '' : ' checked="checked"') . ' /></td>' . "\n"
  1462.                            . '            <td><label for="checkbox_sel_users_' . $index_checkbox . '">' . (empty($host['User']) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($host['User'])) . '</label></td>' . "\n"
  1463.                            . '            <td>' . htmlspecialchars($host['Host']) . '</td>' . "\n";
  1464.                         echo '            <td>';
  1465.                         switch ($host['Password']) {
  1466.                             case 'Y':
  1467.                                 echo $GLOBALS['strYes'];
  1468.                                 break;
  1469.                             case 'N':
  1470.                                 echo '<span style="color: #FF0000">' . $GLOBALS['strNo'] . '</span>';
  1471.                                 break;
  1472.                             // this happens if this is a definition not coming from mysql.user
  1473.                             default:
  1474.                                 echo '--'; // in future version, replace by "not present"
  1475.                                 break;
  1476.                         } // end switch
  1477.                         echo '</td>' . "\n"
  1478.                            . '            <td><tt>' . "\n"
  1479.                            . '                ' . implode( ',' . "\n" . '            ', $host['privs'] ) . "\n"
  1480.                            . '                </tt></td>' . "\n"
  1481.                            . '            <td>' . ($host['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
  1482.                            . '            <td align="center">';
  1483.                         printf( $link_edit, urlencode( $host['User'] ),
  1484.                             urlencode( $host['Host'] ), '', ''  );
  1485.                         echo '</td>' . "\n"
  1486.                            . '        </tr>' . "\n";
  1487.                         $odd_row = ! $odd_row;
  1488.                     }
  1489.                 }
  1490.  
  1491.                 unset( $user, $host, $odd_row );
  1492.                 echo '    </tbody></table>' . "\n"
  1493.                    .'<img class="selectallarrow"'
  1494.                    .' src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png"'
  1495.                    .' width="38" height="22"'
  1496.                    .' alt="' . $GLOBALS['strWithChecked'] . '" />' . "\n"
  1497.                    .'<a href="./server_privileges.php?' . $GLOBALS['url_query'] .  '&checkall=1"'
  1498.                    .' onclick="if ( markAllRows(\'usersForm\') ) return false;">'
  1499.                    . $GLOBALS['strCheckAll'] . '</a>' . "\n"
  1500.                    .'/' . "\n"
  1501.                    .'<a href="server_privileges.php?' . $GLOBALS['url_query'] .  '"'
  1502.                    .' onclick="if ( unMarkAllRows(\'usersForm\') ) return false;">'
  1503.                    . $GLOBALS['strUncheckAll'] . '</a>' . "\n";
  1504.  
  1505.                 // add/delete user fieldset
  1506.                 echo '    <fieldset id="fieldset_add_user">' . "\n"
  1507.                    . '        <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&adduser=1">' . "\n"
  1508.                    . ($GLOBALS['cfg']['PropertiesIconic'] ? '            <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '' )
  1509.                    . '            ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
  1510.                    . '    </fieldset>' . "\n"
  1511.                    . '    <fieldset id="fieldset_delete_user">'
  1512.                    . '        <legend>' . "\n"
  1513.                    . ($GLOBALS['cfg']['PropertiesIconic'] ? '            <img class="icon" src="' . $pmaThemeImage . 'b_usrdrop.png" width="16" height="16" alt="" />' . "\n" : '' )
  1514.                    . '            ' . $GLOBALS['strRemoveSelectedUsers'] . '' . "\n"
  1515.                    . '        </legend>' . "\n";
  1516.  
  1517.                 // before MySQL 4.1.1, we offer some choices for the delete
  1518.                 // mode, but for 4.1.1+, it will be done with REVOKEing the
  1519.                 // privileges then a DROP USER (even no REVOKE at all
  1520.                 // for MySQL 5), so no need to offer so many options
  1521.                 if (PMA_MYSQL_INT_VERSION < 40101) {
  1522.                    echo '        <input type="radio" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '" name="mode" id="radio_mode_1" value="1" checked="checked" />' . "\n"
  1523.                    . '        <label for="radio_mode_1" title="' . $GLOBALS['strJustDelete'] . ' ' . $GLOBALS['strJustDeleteDescr'] . '">' . "\n"
  1524.                    . '            ' . $GLOBALS['strJustDelete'] . "\n"
  1525.                    . '        </label><br />' . "\n"
  1526.                    . '        <input type="radio" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '" name="mode" id="radio_mode_2" value="2" />' . "\n"
  1527.                    . '        <label for="radio_mode_2" title="' . $GLOBALS['strRevokeAndDelete'] . ' ' . $GLOBALS['strRevokeAndDeleteDescr'] . '">' . "\n"
  1528.                    . '            ' . $GLOBALS['strRevokeAndDelete'] . "\n"
  1529.                    . '        </label><br />' . "\n"
  1530.                    . '        <input type="radio" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '" name="mode" id="radio_mode_3" value="3" />' . "\n"
  1531.                    . '        <label for="radio_mode_3" title="' . $GLOBALS['strDeleteAndFlush'] . ' ' . $GLOBALS['strDeleteAndFlushDescr'] . '">' . "\n"
  1532.                    . '            ' . $GLOBALS['strDeleteAndFlush'] . "\n"
  1533.                    . '        </label><br />' . "\n";
  1534.                  } else {
  1535.                      echo '        <input type="hidden" name="mode" value="2" />' . "\n"
  1536.                      . '( ' . $GLOBALS['strRevokeAndDelete'] . ' )<br />' . "\n";
  1537.                  }
  1538.  
  1539.                  echo '        <input type="checkbox" title="' . $GLOBALS['strDropUsersDb'] . '" name="drop_users_db" id="checkbox_drop_users_db" />' . "\n"
  1540.                    . '        <label for="checkbox_drop_users_db" title="' . $GLOBALS['strDropUsersDb'] . '">' . "\n"
  1541.                    . '            ' . $GLOBALS['strDropUsersDb'] . "\n"
  1542.                    . '        </label>' . "\n"
  1543.                    . '    </fieldset>' . "\n"
  1544.                    . '    <fieldset id="fieldset_delete_user_footer" class="tblFooters">' . "\n"
  1545.                    . '        <input type="submit" name="delete" value="' . $GLOBALS['strGo'] . '" id="buttonGo" />' . "\n"
  1546.                    . '    </fieldset>' . "\n";
  1547.             } else {
  1548.  
  1549.                 unset ($row);
  1550.                 echo '    <fieldset id="fieldset_add_user">' . "\n"
  1551.                    . '        <a href="server_privileges.php?' . $GLOBALS['url_query'] . '&adduser=1">' . "\n"
  1552.                    . ($GLOBALS['cfg']['PropertiesIconic'] ? '            <img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' . "\n" : '' )
  1553.                    . '            ' . $GLOBALS['strAddUser'] . '</a>' . "\n"
  1554.                    . '    </fieldset>' . "\n";
  1555.             } // end if (display overview)
  1556.             echo '</form>' . "\n"
  1557.                . '<div class="warning">' . "\n"
  1558.                . '    ' . sprintf($GLOBALS['strFlushPrivilegesNote'], '<a href="server_privileges.php?' . $GLOBALS['url_query'] . '&flush_privileges=1">', '</a>') . "\n"
  1559.                . '</div>' . "\n";
  1560.          }
  1561.  
  1562.  
  1563.     } else {
  1564.  
  1565.         // A user was selected -> display the user's properties
  1566.  
  1567.         echo '<h2>' . "\n"
  1568.            . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usredit.png" width="16" height="16" alt="" />' : '' )
  1569.            . $GLOBALS['strUser'] . ' <i><a href="server_privileges.php?' . $GLOBALS['url_query'] . '&username=' . urlencode($username) . '&hostname=' . urlencode($hostname) . '">\'' . htmlspecialchars($username) . '\'@\'' . htmlspecialchars($hostname) . '\'</a></i>' . "\n";
  1570.         if ( isset( $dbname ) && strlen($dbname) ) {
  1571.             if ( $dbname_is_wildcard ) {
  1572.             echo '    - ' . $GLOBALS['strDatabases'];
  1573.             } else {
  1574.             echo '    - ' . $GLOBALS['strDatabase'];
  1575.             }
  1576.             echo ' <i><a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . $GLOBALS['url_query'] . '&db=' . urlencode($dbname) . '&reload=1">' . htmlspecialchars($dbname) . '</a></i>' . "\n";
  1577.             if ( isset( $tablename ) && strlen($tablename) ) {
  1578.                 echo '    - ' . $GLOBALS['strTable'] . ' <i><a href="' . $GLOBALS['cfg']['DefaultTabTable'] . '?' . $GLOBALS['url_query'] . '&db=' . urlencode($dbname) . '&table=' . urlencode($tablename) . '&reload=1">' . htmlspecialchars($tablename) . '</a></i>' . "\n";
  1579.             }
  1580.         }
  1581.         echo ' : ' . $GLOBALS['strEditPrivileges'] . '</h2>' . "\n";
  1582.         $res = PMA_DBI_query('SELECT \'foo\' FROM `mysql`.`user` WHERE ' . PMA_convert_using('User') . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted') . ' AND ' . PMA_convert_using('Host') . ' = ' . PMA_convert_using($hostname, 'quoted') . ';', null, PMA_DBI_QUERY_STORE);
  1583.         $user_does_not_exists = (PMA_DBI_num_rows( $res ) < 1);
  1584.         PMA_DBI_free_result($res);
  1585.         unset($res);
  1586.         if ( $user_does_not_exists ) {
  1587.             echo $GLOBALS['strUserNotFound'];
  1588.             PMA_displayLoginInformationFields();
  1589.             //require_once('./libraries/footer.inc.php');
  1590.         }
  1591.         echo '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post">' . "\n"
  1592.            . PMA_generate_common_hidden_inputs('', '', 3)
  1593.            . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
  1594.            . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n";
  1595.         if ( isset( $dbname ) && strlen($dbname) ) {
  1596.             echo '<input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '" />' . "\n";
  1597.             if ( isset( $tablename ) && strlen($tablename) ) {
  1598.                 echo ' <input type="hidden" name="tablename" value="' . htmlspecialchars($tablename) . '" />' . "\n";
  1599.             }
  1600.         }
  1601.         PMA_displayPrivTable((( ! isset( $dbname ) || ! strlen($dbname) ) ? '*' : $dbname),
  1602.             ((( ! isset( $dbname ) || ! strlen($dbname) ) || ( ! isset( $tablename ) || ! strlen($tablename) )) ? '*' : $tablename),
  1603.             TRUE, 3);
  1604.         echo '</form>' . "\n";
  1605.  
  1606.         if ( ( ! isset( $tablename ) || ! strlen($tablename) ) && empty( $dbname_is_wildcard ) ) {
  1607.  
  1608.             // no table name was given, display all table specific rights
  1609.             // but only if $dbname contains no wildcards
  1610.  
  1611.             // table header
  1612.             echo '<form action="server_privileges.php" method="post">' . "\n"
  1613.                . PMA_generate_common_hidden_inputs('', '', 6)
  1614.                . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
  1615.                . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
  1616.                . '<fieldset>' . "\n"
  1617.                . '<legend>' . (( ! isset( $dbname ) || ! strlen($dbname) ) ? $GLOBALS['strDbPrivileges'] : $GLOBALS['strTblPrivileges']) . '</legend>' . "\n"
  1618.                . '<table class="data">' . "\n"
  1619.                . '<thead>' . "\n"
  1620.                . '<tr><th>' . (( ! isset( $dbname ) || ! strlen($dbname) ) ? $GLOBALS['strDatabase'] : $GLOBALS['strTable']) . '</th>' . "\n"
  1621.                . '    <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
  1622.                . '    <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
  1623.                . '    <th>' . (( ! isset( $dbname ) || ! strlen($dbname) ) ? $GLOBALS['strTblPrivileges'] : $GLOBALS['strColumnPrivileges']) . '</th>' . "\n"
  1624.                . '    <th colspan="2">' . $GLOBALS['strAction'] . '</th>' . "\n"
  1625.                . '</tr>' . "\n"
  1626.                . '</thead>' . "\n"
  1627.                . '<tbody>' . "\n";
  1628.  
  1629.             $user_host_condition =
  1630.                 ' WHERE ' . PMA_convert_using('`User`')
  1631.                 . ' = ' . PMA_convert_using(PMA_sqlAddslashes($username), 'quoted')
  1632.                 . ' AND ' . PMA_convert_using('`Host`')
  1633.                 . ' = ' . PMA_convert_using($hostname, 'quoted');
  1634.  
  1635.             // table body
  1636.             // get data
  1637.  
  1638.             // we also want privielgs for this user not in table `db` but in other table
  1639.             $tables = PMA_DBI_fetch_result('SHOW TABLES FROM `mysql`;');
  1640.             if ( ( ! isset( $dbname ) || ! strlen($dbname) ) ) {
  1641.  
  1642.                 // no db name given, so we want all privs for the given user
  1643.  
  1644.                 $tables_to_search_for_users = array(
  1645.                     'tables_priv', 'columns_priv',
  1646.                 );
  1647.  
  1648.                 $db_rights_sqls = array();
  1649.                 foreach ( $tables_to_search_for_users as $table_search_in ) {
  1650.                     if ( in_array( $table_search_in, $tables ) ) {
  1651.                         $db_rights_sqls[] = '
  1652.                             SELECT DISTINCT `Db`
  1653.                                    FROM `mysql`.`' . $table_search_in . '`
  1654.                                   ' . $user_host_condition;
  1655.                     }
  1656.                 }
  1657.  
  1658.                 $user_defaults = array(
  1659.                     'Db'          => '',
  1660.                     'Grant_priv'  => 'N',
  1661.                     'privs'       => array( 'USAGE' ),
  1662.                     'Table_privs' => true,
  1663.                 );
  1664.  
  1665.                 // for the rights
  1666.                 $db_rights = array();
  1667.  
  1668.                 if ( PMA_MYSQL_INT_VERSION >= 40000 ) {
  1669.                     $db_rights_sql = '(' . implode( ') UNION (', $db_rights_sqls ) . ')'
  1670.                         .' ORDER BY `Db` ASC';
  1671.  
  1672.                     $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1673.  
  1674.                     while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1675.                         $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1676.                         // only Db names in the table `mysql`.`db` uses wildcards
  1677.                         // as we are in the db specific rights display we want
  1678.                         // all db names escaped, also from other sources
  1679.                         $db_rights_row['Db'] = PMA_escape_mysql_wildcards(
  1680.                             $db_rights_row['Db'] );
  1681.                         $db_rights[$db_rights_row['Db']] = $db_rights_row;
  1682.                     }
  1683.                 } else {
  1684.                     foreach ( $db_rights_sqls as $db_rights_sql ) {
  1685.                         $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1686.  
  1687.                         while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1688.                             $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1689.                             $db_rights[$db_rights_row['Db']] = $db_rights_row;
  1690.                         }
  1691.                     }
  1692.                 }
  1693.                 PMA_DBI_free_result( $db_rights_result );
  1694.                 unset( $db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row );
  1695.  
  1696.                 $sql_query = 'SELECT * FROM `mysql`.`db`' . $user_host_condition . ' ORDER BY `Db` ASC';
  1697.                 $res = PMA_DBI_query( $sql_query );
  1698.                 unset( $sql_query );
  1699.  
  1700.                 while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
  1701.                     if ( isset( $db_rights[$row['Db']] ) ) {
  1702.                         $db_rights[$row['Db']] = array_merge( $db_rights[$row['Db']], $row );
  1703.                     } else {
  1704.                         $db_rights[$row['Db']] = $row;
  1705.                     }
  1706.                     // there are db specific rights for this user
  1707.                     // so we can drop this db rights
  1708.                     $db_rights[$row['Db']]['can_delete'] = true;
  1709.                 }
  1710.                 PMA_DBI_free_result( $res );
  1711.                 unset( $row, $res );
  1712.  
  1713.             } else {
  1714.  
  1715.                 // db name was given,
  1716.                 // so we want all user specific rights for this db
  1717.  
  1718.                 $user_host_condition .=
  1719.                     ' AND ' . PMA_convert_using('`Db`')
  1720.                     .' LIKE ' . PMA_convert_using( $dbname, 'quoted' );
  1721.  
  1722.                 $tables_to_search_for_users = array(
  1723.                     'columns_priv',
  1724.                 );
  1725.  
  1726.                 $db_rights_sqls = array();
  1727.                 foreach ( $tables_to_search_for_users as $table_search_in ) {
  1728.                     if ( in_array( $table_search_in, $tables ) ) {
  1729.                         $db_rights_sqls[] = '
  1730.                             SELECT DISTINCT `Table_name`
  1731.                                    FROM `mysql`.`' . $table_search_in . '`
  1732.                                   ' . $user_host_condition;
  1733.                     }
  1734.                 }
  1735.  
  1736.                 $user_defaults = array(
  1737.                     'Table_name'  => '',
  1738.                     'Grant_priv'  => 'N',
  1739.                     'privs'       => array( 'USAGE' ),
  1740.                     'Column_priv' => true,
  1741.                 );
  1742.  
  1743.                 // for the rights
  1744.                 $db_rights = array();
  1745.  
  1746.                 if ( PMA_MYSQL_INT_VERSION >= 40000 ) {
  1747.                     $db_rights_sql = '(' . implode( ') UNION (', $db_rights_sqls ) . ')'
  1748.                         .' ORDER BY `Table_name` ASC';
  1749.  
  1750.                     $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1751.  
  1752.                     while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1753.                         $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1754.                         $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
  1755.                     }
  1756.                 } else {
  1757.                     foreach ( $db_rights_sqls as $db_rights_sql ) {
  1758.                         $db_rights_result = PMA_DBI_query( $db_rights_sql );
  1759.  
  1760.                         while ( $db_rights_row = PMA_DBI_fetch_assoc( $db_rights_result ) ) {
  1761.                             $db_rights_row = array_merge( $user_defaults, $db_rights_row );
  1762.                             $db_rights[$db_rights_row['Table_name']] = $db_rights_row;
  1763.                         }
  1764.                     }
  1765.                 }
  1766.                 PMA_DBI_free_result( $db_rights_result );
  1767.                 unset( $db_rights_sql, $db_rights_sqls, $db_rights_result, $db_rights_row );
  1768.  
  1769.                 $sql_query =
  1770.                     'SELECT `Table_name`,'
  1771.                     .' `Table_priv`,'
  1772.                     .' IF(`Column_priv` = ' . (PMA_MYSQL_INT_VERSION >= 40100 ? '_latin1 ' : '') . ' \'\', 0, 1)'
  1773.                     .' AS \'Column_priv\''
  1774.                     .' FROM `mysql`.`tables_priv`'
  1775.                     . $user_host_condition
  1776.                     .' ORDER BY `Table_name` ASC;';
  1777.                 $res = PMA_DBI_query( $sql_query );
  1778.                 unset( $sql_query );
  1779.  
  1780.                 while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
  1781.                     if ( isset( $db_rights[$row['Table_name']] ) ) {
  1782.                         $db_rights[$row['Table_name']] = array_merge( $db_rights[$row['Table_name']], $row );
  1783.                     } else {
  1784.                         $db_rights[$row['Table_name']] = $row;
  1785.                     }
  1786.                 }
  1787.                 PMA_DBI_free_result( $res );
  1788.                 unset( $row, $res );
  1789.             }
  1790.             ksort( $db_rights );
  1791.  
  1792.             // display rows
  1793.             if ( count( $db_rights ) < 1 ) {
  1794.                 echo '<tr class="odd">' . "\n"
  1795.                    . '    <td colspan="6"><center><i>' . $GLOBALS['strNone'] . '</i></center></td>' . "\n"
  1796.                    . '</tr>' . "\n";
  1797.             } else {
  1798.                 $odd_row = true;
  1799.                 $found_rows = array();
  1800.                 //while ( $row = PMA_DBI_fetch_assoc( $res ) ) {
  1801.                 foreach ( $db_rights as $row ) {
  1802.                     $found_rows[] = ( ! isset( $dbname ) || ! strlen($dbname) ) ? $row['Db'] : $row['Table_name'];
  1803.  
  1804.                     echo '<tr class="' . ( $odd_row ? 'odd' : 'even' ) . '">' . "\n"
  1805.                        . '    <td>' . htmlspecialchars(( ! isset( $dbname ) || ! strlen($dbname) ) ? $row['Db'] : $row['Table_name']) . '</td>' . "\n"
  1806.                        . '    <td><tt>' . "\n"
  1807.                        . '        ' . join(',' . "\n" . '            ', PMA_extractPrivInfo($row, TRUE)) . "\n"
  1808.                        . '        </tt></td>' . "\n"
  1809.                        . '    <td>' . (((( ! isset( $dbname ) || ! strlen($dbname) ) && $row['Grant_priv'] == 'Y') || (isset($dbname) && strlen($dbname) && in_array('Grant', explode(',', $row['Table_priv'])))) ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . '</td>' . "\n"
  1810.                        . '    <td>';
  1811.                     if ( ! empty( $row['Table_privs'] ) || ! empty ( $row['Column_priv'] ) ) {
  1812.                         echo $GLOBALS['strYes'];
  1813.                     } else {
  1814.                         echo $GLOBALS['strNo'];
  1815.                     }
  1816.                     echo '</td>' . "\n"
  1817.                        . '    <td>';
  1818.                     printf( $link_edit, urlencode( $username ),
  1819.                         urlencode( $hostname ),
  1820.                         urlencode( ( ! isset( $dbname ) || ! strlen($dbname) ) ? $row['Db'] : $dbname ),
  1821.                         urlencode( ( ! isset( $dbname ) || ! strlen($dbname) ) ? '' : $row['Table_name'] ) );
  1822.                     echo '</td>' . "\n"
  1823.                        . '    <td>';
  1824.                     if ( ! empty( $row['can_delete'] ) || isset( $row['Table_name'] ) && strlen($row['Table_name']) ) {
  1825.                         printf( $link_revoke, urlencode( $username ),
  1826.                             urlencode( $hostname ),
  1827.                             urlencode( ( ! isset( $dbname ) || ! strlen($dbname) ) ? $row['Db'] : $dbname ),
  1828.                             urlencode( ( ! isset( $dbname ) || ! strlen($dbname) ) ? '' : $row['Table_name'] ) );
  1829.                     }
  1830.                     echo '</td>' . "\n"
  1831.                        . '</tr>' . "\n";
  1832.                     $odd_row = ! $odd_row;
  1833.                 } // end while
  1834.             }
  1835.             unset($row);
  1836.             echo '</tbody>' . "\n"
  1837.                . '</table>' . "\n";
  1838.  
  1839.             if ( ! isset( $dbname ) || ! strlen($dbname) ) {
  1840.  
  1841.                 // no database name was give, display select db
  1842.  
  1843.                 if ( ! empty( $found_rows ) ) {
  1844.                     $pred_db_array = array_diff(
  1845.                         PMA_DBI_fetch_result( 'SHOW DATABASES;' ),
  1846.                         $found_rows );
  1847.                 } else {
  1848.                     $pred_db_array =PMA_DBI_fetch_result( 'SHOW DATABASES;' );
  1849.                 }
  1850.  
  1851.                 echo '    <label for="text_dbname">' . $GLOBALS['strAddPrivilegesOnDb'] . ':</label>' . "\n";
  1852.                 if (!empty($pred_db_array)) {
  1853.                     echo '    <select name="pred_dbname" onchange="this.form.submit();">' . "\n"
  1854.                        . '        <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
  1855.                     foreach ($pred_db_array as $current_db) {
  1856.                         $current_db = PMA_escape_mysql_wildcards( $current_db );
  1857.                         echo '        <option value="' . htmlspecialchars( $current_db ) . '">'
  1858.                             . htmlspecialchars( $current_db ) . '</option>' . "\n";
  1859.                     }
  1860.                     echo '    </select>' . "\n";
  1861.                 }
  1862.                 echo '    <input type="text" id="text_dbname" name="dbname" />' . "\n"
  1863.                     .PMA_showHint( $GLOBALS['strEscapeWildcards'] );
  1864.             } else {
  1865.                 echo '    <input type="hidden" name="dbname" value="' . htmlspecialchars($dbname) . '"/>' . "\n"
  1866.                    . '    <label for="text_tablename">' . $GLOBALS['strAddPrivilegesOnTbl'] . ':</label>' . "\n";
  1867.                 if ($res = @PMA_DBI_try_query('SHOW TABLES FROM ' . PMA_backquote($dbname) . ';', null, PMA_DBI_QUERY_STORE)) {
  1868.                     $pred_tbl_array = array();
  1869.                     while ($row = PMA_DBI_fetch_row($res)) {
  1870.                         if (!isset($found_rows) || !in_array($row[0], $found_rows)) {
  1871.                             $pred_tbl_array[] = $row[0];
  1872.                         }
  1873.                     }
  1874.                     PMA_DBI_free_result($res);
  1875.                     unset( $res, $row );
  1876.                     if (!empty($pred_tbl_array)) {
  1877.                         echo '    <select name="pred_tablename" onchange="this.form.submit();">' . "\n"
  1878.                            . '        <option value="" selected="selected">' . $GLOBALS['strUseTextField'] . ':</option>' . "\n";
  1879.                         foreach ($pred_tbl_array as $current_table) {
  1880.                             echo '        <option value="' . htmlspecialchars($current_table) . '">' . htmlspecialchars($current_table) . '</option>' . "\n";
  1881.                         }
  1882.                         echo '    </select>' . "\n";
  1883.                     }
  1884.                 } else {
  1885.                     unset($res);
  1886.                 }
  1887.                 echo '    <input type="text" id="text_tablename" name="tablename" />' . "\n";
  1888.             }
  1889.             echo '</fieldset>' . "\n";
  1890.             echo '<fieldset class="tblFooters">' . "\n"
  1891.                . '    <input type="submit" value="' . $GLOBALS['strGo'] . '" />'
  1892.                . '</fieldset>' . "\n"
  1893.                . '</form>' . "\n";
  1894.         }
  1895.  
  1896.         if ( ( ! isset( $dbname ) || ! strlen($dbname) ) && ! $user_does_not_exists ) {
  1897.             echo '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
  1898.                . PMA_generate_common_hidden_inputs('', '', 3)
  1899.                . '<input type="hidden" name="username" value="' . htmlspecialchars($username) . '" />' . "\n"
  1900.                . '<input type="hidden" name="hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
  1901.                . '<fieldset id="fieldset_change_password">' . "\n"
  1902.                . '    <legend>' . $GLOBALS['strChangePassword'] . '</legend>' . "\n"
  1903.                . '    <table class="data">' . "\n"
  1904.                . '    <tr class="odd noclick">' . "\n"
  1905.                . '        <td><input type="radio" name="nopass" value="1" id="radio_nopass_1" onclick="pw_pma_pw.value=\'\'; pw_pma_pw2.value=\'\';" /></td>' . "\n"
  1906.                . '        <td colspan="2"><label for="radio_nopass_1">' . $GLOBALS['strNoPassword'] . '</label></td>' . "\n"
  1907.                . '    </tr>' . "\n"
  1908.                . '    <tr class="even noclick">' . "\n"
  1909.                . '        <td><input type="radio" name="nopass" value="0" id="radio_nopass_0" onclick="document.getElementById(\'pw_pma_pw\').focus();" /></td>' . "\n"
  1910.                . '        <td><label for="radio_nopass_0">' . $GLOBALS['strPassword'] . ':</label></td>' . "\n"
  1911.                . '        <td><input type="password" name="pma_pw" id="pw_pma_pw" onchange="nopass[1].checked = true;" /></td>' . "\n"
  1912.                . '    </tr>' . "\n"
  1913.                . '    <tr class="odd noclick">' . "\n"
  1914.                . '        <td></td>' . "\n"
  1915.                . '        <td><label for="pw_pma_pw2">' . $GLOBALS['strReType'] . ':</label></td>' . "\n"
  1916.                . '        <td><input type="password" name="pma_pw2" id="pw_pma_pw2" onchange="nopass[1].checked = true;" /></td>' . "\n"
  1917.                . '    </tr>' . "\n"
  1918.                . '    </table>' . "\n"
  1919.                . '</fieldset>' . "\n"
  1920.                . '<fieldset id="fieldset_change_password_footer" class="tblFooters">' . "\n"
  1921.                . '    <input type="submit" name="change_pw" value="' . $GLOBALS['strGo'] . '" />' . "\n"
  1922.                . '</fieldset>' . "\n"
  1923.                . '</form>' . "\n"
  1924.                . '<form action="server_privileges.php" method="post" onsubmit="return checkPassword(this);">' . "\n"
  1925.                . PMA_generate_common_hidden_inputs('', '', 3)
  1926.                . '<input type="hidden" name="old_username" value="' . htmlspecialchars($username) . '" />' . "\n"
  1927.                . '<input type="hidden" name="old_hostname" value="' . htmlspecialchars($hostname) . '" />' . "\n"
  1928.                . '<fieldset id="fieldset_change_copy_user">' . "\n"
  1929.                . '    <legend>' . $GLOBALS['strChangeCopyUser'] . '</legend>' . "\n";
  1930.             PMA_displayLoginInformationFields('change', 3 );
  1931.             echo '    <fieldset>' . "\n"
  1932.                . '        <legend>' . $GLOBALS['strChangeCopyMode'] . '</legend>' . "\n"
  1933.                . '        <input type="radio" name="mode" value="4" id="radio_mode_4" checked="checked" /><label for="radio_mode_4">' . "\n"
  1934.                . '            ' . $GLOBALS['strChangeCopyModeCopy'] . "\n"
  1935.                . '        </label><br />' . "\n"
  1936.                . '        <input type="radio" name="mode" value="1" id="radio_mode_1" /><label for="radio_mode_1">' . "\n"
  1937.                . '            ' . $GLOBALS['strChangeCopyModeJustDelete'] . "\n"
  1938.                . '        </label><br />' . "\n"
  1939.                . '        <input type="radio" name="mode" value="2" id="radio_mode_2" /><label for="radio_mode_2">' . "\n"
  1940.                . '            ' . $GLOBALS['strChangeCopyModeRevoke'] . "\n"
  1941.                . '        </label><br />' . "\n"
  1942.                . '        <input type="radio" name="mode" value="3" id="radio_mode_3" /><label for="radio_mode_3">' . "\n"
  1943.                . '            ' . $GLOBALS['strChangeCopyModeDeleteAndReload'] . "\n"
  1944.                . '        </label>' . "\n"
  1945.                . '    </fieldset>' . "\n"
  1946.                . '</fieldset>' . "\n"
  1947.                . '<fieldset id="fieldset_change_copy_user_footer" class="tblFooters">' . "\n"
  1948.                . '    <input type="submit" name="change_copy" value="' . $GLOBALS['strGo'] . '" />' . "\n"
  1949.                . '</fieldset>' . "\n"
  1950.                . '</form>' . "\n";
  1951.         }
  1952.     }
  1953. } elseif (!empty($adduser)) {
  1954.     // Add a new user
  1955.     $GLOBALS['url_query'] .= '&adduser=1';
  1956.     echo '<h2>' . "\n"
  1957.        . ($GLOBALS['cfg']['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_usradd.png" width="16" height="16" alt="" />' : '' )
  1958.        . '    ' . $GLOBALS['strAddUser'] . "\n"
  1959.        . '</h2>' . "\n"
  1960.        . '<form name="usersForm" id="usersForm" action="server_privileges.php" method="post" onsubmit="return checkAddUser(this);">' . "\n"
  1961.        . PMA_generate_common_hidden_inputs('', '', 1);
  1962.     PMA_displayLoginInformationFields('new', 2);
  1963.     echo '<fieldset id="fieldset_add_user_database">' . "\n"
  1964.        . '<legend>' . $GLOBALS['strCreateUserDatabase'] . '</legend>' . "\n"
  1965.        . '    <div class="item">' . "\n"
  1966.        . '        <input type="radio" name="createdb" value="0" id="radio_createdb_0" checked="checked" />' . "\n"
  1967.        . '        <label for="radio_createdb_0">' . $GLOBALS['strCreateUserDatabaseNone'] . '</label>' . "\n"
  1968.        . '    </div>' . "\n"
  1969.        . '    <div class="item">' . "\n"
  1970.        . '        <input type="radio" name="createdb" value="1" id="radio_createdb_1" />' . "\n"
  1971.        . '        <label for="radio_createdb_1">' . $GLOBALS['strCreateUserDatabaseName'] . '</label>' . "\n"
  1972.        . '    </div>' . "\n"
  1973.        . '    <div class="item">' . "\n"
  1974.        . '        <input type="radio" name="createdb" value="2" id="radio_createdb_2" />' . "\n"
  1975.        . '        <label for="radio_createdb_2">' . $GLOBALS['strCreateUserDatabaseWildcard'] . '</label>' . "\n"
  1976.        . '    </div>' . "\n"
  1977.        . '</fieldset>' . "\n";
  1978.     PMA_displayPrivTable('*', '*', FALSE, 1);
  1979.     echo '    <fieldset id="fieldset_add_user_footer" class="tblFooters">' . "\n"
  1980.        . '        <input type="submit" name="adduser_submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
  1981.        . '    </fieldset>' . "\n"
  1982.        . '</form>' . "\n";
  1983. } else {
  1984.     // check the privileges for a particular database.
  1985.     echo '<table id="tablespecificuserrights" class="data">' . "\n"
  1986.        . '<caption class="tblHeaders">' . "\n"
  1987.        . ($GLOBALS['cfg']['PropertiesIconic'] ? '            <img class="icon" src="' . $pmaThemeImage . 'b_usrcheck.png" width="16" height="16" alt="" />' . "\n" : '' )
  1988.        . '    ' . sprintf($GLOBALS['strUsersHavingAccessToDb'], '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase'] . '?' . PMA_generate_common_url($checkprivs) . '">' .  htmlspecialchars($checkprivs) . '</a>') . "\n"
  1989.        . '</caption>' . "\n"
  1990.        . '<thead>' . "\n"
  1991.        . '    <tr><th>' . $GLOBALS['strUser'] . '</th>' . "\n"
  1992.        . '        <th>' . $GLOBALS['strHost'] . '</th>' . "\n"
  1993.        . '        <th>' . $GLOBALS['strType'] . '</th>' . "\n"
  1994.        . '        <th>' . $GLOBALS['strPrivileges'] . '</th>' . "\n"
  1995.        . '        <th>' . $GLOBALS['strGrantOption'] . '</th>' . "\n"
  1996.        . '        <th>' . $GLOBALS['strAction'] . '</th>' . "\n"
  1997.        . '    </tr>' . "\n"
  1998.        . '</thead>' . "\n"
  1999.        . '<tbody>' . "\n";
  2000.     $odd_row = TRUE;
  2001.     unset($row);
  2002.     unset($row1);
  2003.     unset($row2);
  2004.     // now, we build the table...
  2005.     if (PMA_MYSQL_INT_VERSION >= 40000) {
  2006.         // Starting with MySQL 4.0.0, we may use UNION SELECTs and this makes
  2007.         // the job much easier here!
  2008.  
  2009.         $no = PMA_convert_using('N', 'quoted');
  2010.  
  2011.         $list_of_privileges =
  2012.             PMA_convert_using('Select_priv') . ' AS Select_priv, '
  2013.             . PMA_convert_using('Insert_priv') . ' AS Insert_priv, '
  2014.             . PMA_convert_using('Update_priv') . ' AS Update_priv, '
  2015.             . PMA_convert_using('Delete_priv') . ' AS Delete_priv, '
  2016.             . PMA_convert_using('Create_priv') . ' AS Create_priv, '
  2017.             . PMA_convert_using('Drop_priv') . ' AS Drop_priv, '
  2018.             . PMA_convert_using('Grant_priv') . ' AS Grant_priv, '
  2019.             . PMA_convert_using('References_priv') . ' AS References_priv';
  2020.  
  2021.         $list_of_compared_privileges =
  2022.             PMA_convert_using('Select_priv') . ' = ' . $no
  2023.             . ' AND ' . PMA_convert_using('Insert_priv') . ' = ' . $no
  2024.             . ' AND ' . PMA_convert_using('Update_priv') . ' = ' . $no
  2025.             . ' AND ' . PMA_convert_using('Delete_priv') . ' = ' . $no
  2026.             . ' AND ' . PMA_convert_using('Create_priv') . ' = ' . $no
  2027.             . ' AND ' . PMA_convert_using('Drop_priv') . ' = ' . $no
  2028.             . ' AND ' . PMA_convert_using('Grant_priv') . ' = ' . $no
  2029.             . ' AND ' . PMA_convert_using('References_priv') . ' = ' . $no;
  2030.  
  2031.         $sql_query =
  2032.             '(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
  2033.             .   PMA_convert_using('`Host`') . ' AS `Host`, '
  2034.             .   PMA_convert_using('`Db`') . ' AS `Db`, '
  2035.             .   $list_of_privileges
  2036.             .' FROM `mysql`.`db`'
  2037.             .' WHERE ' . PMA_convert_using( $checkprivs, 'quoted' )
  2038.             .' LIKE ' . PMA_convert_using('`Db`')
  2039.             .' AND NOT (' . $list_of_compared_privileges. ')) '
  2040.             .'UNION '
  2041.             .'(SELECT ' . PMA_convert_using('`User`') . ' AS `User`, '
  2042.             .   PMA_convert_using('`Host`') . ' AS `Host`, '
  2043.             .   PMA_convert_using('*', 'quoted') .' AS `Db`, '
  2044.             .   $list_of_privileges
  2045.             .' FROM `mysql`.`user` '
  2046.             .' WHERE NOT (' . $list_of_compared_privileges . ')) '
  2047.             .' ORDER BY `User` ASC,'
  2048.             .'  `Host` ASC,'
  2049.             .'  `Db` ASC;';
  2050.         $res = PMA_DBI_query($sql_query);
  2051.         $row = PMA_DBI_fetch_assoc($res);
  2052.         if ($row) {
  2053.             $found = TRUE;
  2054.         }
  2055.     } else {
  2056.         // With MySQL 3, we need 2 seperate queries here.
  2057.         $sql_query = 'SELECT * FROM `mysql`.`user` WHERE NOT (`Select_priv` = \'N\' AND `Insert_priv` = \'N\' AND `Update_priv` = \'N\' AND `Delete_priv` = \'N\' AND `Create_priv` = \'N\' AND `Drop_priv` = \'N\' AND `Grant_priv` = \'N\' AND `References_priv` = \'N\') ORDER BY `User` ASC, `Host` ASC;';
  2058.         $res1 = PMA_DBI_query($sql_query);
  2059.         $row1 = PMA_DBI_fetch_assoc($res1);
  2060.         $sql_query =
  2061.             'SELECT * FROM `mysql`.`db`'
  2062.             .' WHERE \'' . $checkprivs . '\''
  2063.             .'   LIKE `Db`'
  2064.             .'  AND NOT (`Select_priv` = \'N\''
  2065.             .'   AND `Insert_priv` = \'N\''
  2066.             .'   AND `Update_priv` = \'N\''
  2067.             .'   AND `Delete_priv` = \'N\''
  2068.             .'   AND `Create_priv` = \'N\''
  2069.             .'   AND `Drop_priv` = \'N\''
  2070.             .'   AND `Grant_priv` = \'N\''
  2071.             .'   AND `References_priv` = \'N\')'
  2072.             .' ORDER BY `User` ASC, `Host` ASC;';
  2073.         $res2 = PMA_DBI_query($sql_query);
  2074.         $row2 = PMA_DBI_fetch_assoc($res2);
  2075.         if ($row1 || $row2) {
  2076.             $found = TRUE;
  2077.         }
  2078.     } // end if (PMA_MYSQL_INT_VERSION >= 40000) ... else ...
  2079.     if ($found) {
  2080.         while (TRUE) {
  2081.             // prepare the current user
  2082.             if (PMA_MYSQL_INT_VERSION >= 40000) {
  2083.                 $current_privileges = array();
  2084.                 $current_user = $row['User'];
  2085.                 $current_host = $row['Host'];
  2086.                 while ($row && $current_user == $row['User'] && $current_host == $row['Host']) {
  2087.                     $current_privileges[] = $row;
  2088.                     $row = PMA_DBI_fetch_assoc($res);
  2089.                 }
  2090.             } else {
  2091.                 $current_privileges = array();
  2092.                 if ($row1 && (!$row2 || ($row1['User'] < $row2['User'] || ($row1['User'] == $row2['User'] && $row1['Host'] <= $row2['Host'])))) {
  2093.                     $current_user = $row1['User'];
  2094.                     $current_host = $row1['Host'];
  2095.                     $current_privileges = array($row1);
  2096.                     $row1 = PMA_DBI_fetch_assoc($res1);
  2097.                 } else {
  2098.                     $current_user = $row2['User'];
  2099.                     $current_host = $row2['Host'];
  2100.                     $current_privileges = array();
  2101.                 }
  2102.                 while ($row2 && $current_user == $row2['User'] && $current_host == $row2['Host']) {
  2103.                     $current_privileges[] = $row2;
  2104.                     $row2 = PMA_DBI_fetch_assoc($res2);
  2105.                 }
  2106.             }
  2107.             echo '    <tr class="' . ( $odd_row ? 'odd' : 'even' ) . '">' . "\n"
  2108.                . '        <td';
  2109.             if (count($current_privileges) > 1) {
  2110.                 echo ' rowspan="' . count($current_privileges) . '"';
  2111.             }
  2112.             echo '>' . (empty($current_user) ? '<span style="color: #FF0000">' . $GLOBALS['strAny'] . '</span>' : htmlspecialchars($current_user)) . "\n"
  2113.                . '        </td>' . "\n"
  2114.                . '        <td';
  2115.             if (count($current_privileges) > 1) {
  2116.                 echo ' rowspan="' . count($current_privileges) . '"';
  2117.             }
  2118.             echo '>' . htmlspecialchars($current_host) . '</td>' . "\n";
  2119.             foreach ($current_privileges as $current) {
  2120.                 echo '        <td>' . "\n"
  2121.                    . '            ';
  2122.                 if (!isset($current['Db']) || $current['Db'] == '*') {
  2123.                     echo $GLOBALS['strGlobal'];
  2124.                 } elseif ( $current['Db'] == PMA_escape_mysql_wildcards( $checkprivs ) ) {
  2125.                     echo $GLOBALS['strDbSpecific'];
  2126.                 } else {
  2127.                     echo $GLOBALS['strWildcard'], ': <tt>' . htmlspecialchars($current['Db']) . '</tt>';
  2128.                 }
  2129.                 echo "\n"
  2130.                    . '        </td>' . "\n"
  2131.                    . '        <td>' . "\n"
  2132.                    . '            <tt>' . "\n"
  2133.                    . '                ' . join(',' . "\n" . '                ', PMA_extractPrivInfo($current, TRUE)) . "\n"
  2134.                    . '            </tt>' . "\n"
  2135.                    . '        </td>' . "\n"
  2136.                    . '        <td>' . "\n"
  2137.                    . '            ' . ($current['Grant_priv'] == 'Y' ? $GLOBALS['strYes'] : $GLOBALS['strNo']) . "\n"
  2138.                    . '        </td>' . "\n"
  2139.                    . '        <td>' . "\n";
  2140.                 printf( $link_edit, urlencode( $current_user ),
  2141.                     urlencode( $current_host ),
  2142.                     urlencode( ! isset( $current['Db'] ) || $current['Db'] == '*' ? '' : $current['Db'] ),
  2143.                     '' );
  2144.                 echo '</td>' . "\n"
  2145.                    . '    </tr>' . "\n";
  2146.             }
  2147.             if (empty($row) && empty($row1) && empty($row2)) {
  2148.                 break;
  2149.             }
  2150.             $odd_row = ! $odd_row;
  2151.         }
  2152.     } else {
  2153.         echo '    <tr class="odd">' . "\n"
  2154.            . '        <td colspan="6">' . "\n"
  2155.            . '            ' . $GLOBALS['strNoUsersFound'] . "\n"
  2156.            . '        </td>' . "\n"
  2157.            . '    </tr>' . "\n";
  2158.     }
  2159.     echo '</tbody>' . "\n"
  2160.        . '</table>' . "\n";
  2161. } // end if (empty($adduser) && empty($checkprivs)) ... elseif ... else ...
  2162.  
  2163.  
  2164. /**
  2165.  * Displays the footer
  2166.  */
  2167. echo "\n\n";
  2168. require_once('./libraries/footer.inc.php');
  2169.  
  2170. ?>
  2171.